Show problems as
Classic problem #2001MediumArrayHash TableMathCountingNumber Theory

Number of Pairs of Interchangeable Rectangles

Real-world version: Panes Cut to the Same Shape
Tracked in this browser only
Read Number of Pairs of Interchangeable Rectangles on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A glazier's stock is given as rectangles, where rectangles[i] is [width, height].

Two panes are the same shape when their width divided by their height comes to the same value.

Return how many pairs of panes in the stock are the same shape.

Examples

Example 1

Input
rectangles = [[24, 36], [14, 21], [90000, 60000], [15, 10], [7, 11]]
Output
2

The first two both reduce to 2 by 3, and the next two both reduce to 3 by 2, giving one pair from each group. The last pane is on its own.

Example 2

Input
rectangles = [[1, 7], [2, 14], [3, 21], [4, 28], [5, 35], [6, 42]]
Output
15

All six panes reduce to 1 by 7, so every one of the fifteen pairs matches.

Example 3

Input
rectangles = [[2, 3], [3, 2]]
Output
0

Turning a pane on its side gives a different proportion, so these two are not the same shape.

Constraints

  • 1 <= rectangles.length <= 10^5
  • rectangles[i].length == 2
  • 1 <= rectangles[i][0] <= 10^5
  • 1 <= rectangles[i][1] <= 10^5

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def interchangeable_rectangles(rectangles: list[list[int]]) -> int:
Java
public long interchangeableRectangles(int[][] rectangles)