Number of Pairs of Interchangeable Rectangles

Medium Topics Company Tags

Each entry [width, height] in rectangles describes one rectangle. Count index pairs i < j whose width-to-height ratios are exactly equal. Dimensions may differ as long as the ratios match. Width and height keep their roles, so rotating a rectangle is not allowed; identical entries at different indices still form separate pairs. Return the total number of matching pairs.

Example 1:

Input: rectangles = [[2,3],[4,6],[6,9]]

Output: 3

Explanation: All three rectangles have ratio 2/3. The index pairs (0,1), (0,2), and (1,2) match, giving 3.

Example 2:

Input: rectangles = [[2,3],[3,2]]

Output: 0

Explanation: The ratios 2/3 and 3/2 differ. Rotation is not allowed, so there are no matching pairs.

Example 3:

Input: rectangles = [[5,5],[5,5],[2,2],[2,3]]

Output: 3

Explanation: The first three entries are squares and each pair among them matches, giving 3 pairs. The last rectangle has a different ratio.

Constraints:

  • 1 <= rectangles.length <= 100000
  • Each rectangle has exactly two entries: [width, height].
  • 1 <= width, height <= 100000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.