Ctrl K

Balanced Quadruples With Gaps

Medium
You get an array of positive integers values. Call four indices p < q < r < s a balanced quadruple if: - values[p] * values[r] == values[q] * values[s]; - every two neighbouring chosen indices have at least one unchosen element between them: q - p ≥ 2, r - q ≥ 2, s - r ≥ 2. Return the number of balanced quadruples. Two quadruples are different if they differ in at least one index.
Example 1
values=[2, 9, 3, 9, 6, 9, 4] → 1

Indices (0, 2, 4, 6) work: 2 · 6 = 3 · 4 = 12, and there is a gap between neighbouring indices. There are no others.

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

With length 7 the only possible choice is (0, 2, 4, 6), and 1 · 5 ≠ 3 · 7.

Example 3
values=[5, 5, 5, 5, 5, 5, 5, 5] → 5
Constraints
  • 7 ≤ len(values) ≤ 1000
  • 1 ≤ values[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsGrid Points Under Circles MediumPrimes Along Grid Rays MediumPrimes Below a Bound MediumVertical Mirror Line MediumEquidistant Point Triples Medium
loading Python…
Input
values=[2, 9, 3, 9, 6, 9, 4]
Expected
1

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