Number of Pairs of Interchangeable Rectangles

Medium

Description

Given rectangle dimensions [width, height], count pairs of rectangles with the same width/height ratio.

Examples

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

All have ratio 1:2.

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

Edge case returning zero.

Input:rectangles = [[2,3],[4,6],[1,5],[3,2],[6,4]]
Output:2
Explanation:

Rectangles are interchangeable if they have the same width/height ratio. [2,3] has ratio 2/3, [4,6] has ratio 4/6=2/3 — one pair. [3,2] has ratio 3/2, [6,4] has ratio 6/4=3/2 — another pair. [1,5] has ratio 1/5 with no match. Total: 2 pairs.

Constraints

  • 1 ≤ rectangles.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!