Back to DSA sheet
Number of Pairs of Interchangeable Rectangles
MediumArraysTwo rectangles are interchangeable if their width-to-height ratios are equal. Count the number of interchangeable pairs.
Input format (stdin): the first line has n. Each of the next n lines has two integers width and height. Output the number of interchangeable pairs.
Examples
Input: 6
4 8
3 6
10 20
15 30
2 1
4 2
Output: 7
Four rectangles share ratio 1/2 (6 pairs) and two share ratio 2 (1 pair), totalling 7.
Constraints
- 1 <= n <= 10^5
- 1 <= width, height <= 10^5
Sheets
NeetCode 250
number-of-pairs-of-interchangeable-rectangles.cpp2 sample tests
Loading editor
Test results
Run the sample tests to check your solution against expected output.
Custom input (stdin)
Output
Run your code to see its output.