Number of Pairs of Interchangeable Rectangles

You are given n rectangles represented by a 0-indexed 2D integer array rectangles, where rectangles[i] = [widthi, heighti] denotes the width and height of the i^th 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.

Example 1
Inputrectangles = [[4,8],[3,6],[10,20],[15,30]]
Output6
All four rectangles have the same width-to-height ratio, so all six possible pairs are interchangeable.
Example 2
Inputrectangles = [[4,5],[7,8]]
Output0
There are no interchangeable pairs of rectangles.

Constraints

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

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate