> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Pairs of Interchangeable Rectangles

> Tested Python solution for LeetCode 2001 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2001, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Counting](/catalog/topics/counting), [Number Theory](/catalog/topics/number-theory). [View on LeetCode](https://leetcode.com/problems/number-of-pairs-of-interchangeable-rectangles/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2001   # by problem number
lcpy gen -s number_of_pairs_of_interchangeable_rectangles   # by problem name
```

## Problem

You are given `n` rectangles represented as a **0-indexed** 2D integer array `rectangles`, where `rectangles[i] = [widthi, heighti]` denotes the width and height of the `ith` rectangle.

Two rectangles `i` and `j` (`i < j`) are considered **interchangeable** if they have the **same** width-to-height ratio. More formally, two rectangles are **interchangeable** if `widthi / heighti == widthj / heightj` (using decimal division, not integer division).

Return *the **number** of pairs of **interchangeable** rectangles* in `rectangles`.

### Examples

```
Input: rectangles = [[4,8],[3,6],[10,20],[15,30]]
Output: 6
```

**Explanation:** The following are the interchangeable pairs of rectangles by index (0-indexed):

* Rectangle 0 with rectangle 1: `4/8 == 3/6`.
* Rectangle 0 with rectangle 2: `4/8 == 10/20`.
* Rectangle 0 with rectangle 3: `4/8 == 15/30`.
* Rectangle 1 with rectangle 2: `3/6 == 10/20`.
* Rectangle 1 with rectangle 3: `3/6 == 15/30`.
* Rectangle 2 with rectangle 3: `10/20 == 15/30`.

```
Input: rectangles = [[4,5],[7,8]]
Output: 0
```

**Explanation:** There are no interchangeable pairs of rectangles.

### Constraints

* `n == rectangles.length`
* `1 <= n <= 10^5`
* `rectangles[i].length == 2`
* `1 <= widthi, heighti <= 10^5`

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_pairs_of_interchangeable_rectangles/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_pairs_of_interchangeable_rectangles/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from math import gcd


class Solution:
    # Time: O(n * log(max(width, height)))
    # Space: O(n)
    def interchangeable_rectangles(self, rectangles: list[list[int]]) -> int:
        counts: dict[tuple[int, int], int] = {}
        pairs = 0
        for width, height in rectangles:
            divisor = gcd(width, height)
            ratio = (width // divisor, height // divisor)
            pairs += counts.get(ratio, 0)
            counts[ratio] = counts.get(ratio, 0) + 1
        return pairs
```

## Complexity

| Time | Space |
| - | - |
| O(n \* log(max(width, height))) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.