Ctrl K

Photo Frames of the Same Shape

Medium
A workshop has n photo frames. Frame i is given as frames[i] = [w, h], its width and height. Two frames have the same shape if their width-to-height ratios are exactly equal, meaning one is a scaled copy of the other. Frames may not be rotated. Return the number of index pairs i < j such that frames i and j have the same shape.
Example 1
frames=[[3, 2], [6, 4], [2, 3], [9, 6]] → 3

Frames 3×2, 6×4 and 9×6 all have ratio 3 : 2 and make three pairs. Frame 2×3 has ratio 2 : 3 and pairs with nobody.

Example 2
frames=[[5, 7], [7, 5]] → 0

The ratios 5 : 7 and 7 : 5 differ, so there are no pairs.

Example 3
frames=[[1, 1], [4, 4], [10, 10]] → 3
Constraints
  • 1 ≤ n ≤ 10⁵
  • 1 ≤ w, h ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPrimes Along Grid Rays MediumEven Piles of Cards EasyCoprime Digit Pairs EasyPair GCDs by Sorted Position HardPairs Balanced by Their Mirrors Medium
loading Python…
Input
frames=[[3, 2], [6, 4], [2, 3], [9, 6]]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 85 tests on the server, hidden ones included.