Ctrl K

Quadruples With Equal Products

Medium
You are given an array values of distinct positive integers. Count the ordered quadruples (a, b, c, d) of four different elements of the array such that a · b = c · d. Quadruples that differ only in order are counted separately: for example, (2, 6, 3, 4) and (6, 2, 3, 4) are two different quadruples.
Example 1
values=[3, 4, 6, 8] → 8

3 · 8 = 4 · 6 = 24. These two pairs give 8 quadruples: (3, 8, 4, 6), (8, 3, 4, 6), (3, 8, 6, 4), (8, 3, 6, 4), plus four more with the pairs swapped.

Example 2
values=[1, 3, 5, 7] → 0

All six pairwise products are different, so there is no valid quadruple.

Example 3
values=[1, 2, 3, 4, 6, 12] → 40
Constraints
  • 1 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] ≤ 10⁴
  • All elements are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMore Than a Third of the Votes MediumMost Common Values MediumJobs With a Cooldown MediumMost Searched Words MediumDomain Visit Rollup Medium
loading Python…
Input
values=[3, 4, 6, 8]
Expected
8

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