Ctrl K

Triples With a Lone Divisor

Medium
You get an array of positive integers values. Look at ordered triples of pairwise distinct indices (i, j, k). A triple counts if the sum values[i] + values[j] + values[k] is divisible by exactly one of the three numbers values[i], values[j], values[k]. The three numbers are judged as separate elements: if two of them are equal and both divide the sum, that is two divisors, not one. Return the number of such triples. Order matters: (0, 1, 2) and (1, 0, 2) are different triples.
Example 1
values=[2, 3, 4] → 6

The sum 2 + 3 + 4 = 9 is divisible by 3 only. The indices 0, 1, 2 work in all 6 orders.

Example 2
values=[5, 5, 1, 2] → 12

5 + 5 + 1 = 11 is divisible only by 1, giving 6 ordered triples; 5 + 5 + 2 = 12 is divisible only by 2, giving 6 more. A triple 5, 1, 2 sums to 8, divisible by both 1 and 2, so it does not count. Total: 12.

Example 3
values=[3, 3, 3] → 0
Constraints
  • 3 ≤ len(values) ≤ 10⁵
  • 1 ≤ values[i] ≤ 100
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumBackwards Digits in 32 Bits MediumNumber in Roman Numerals MediumDivision Without Dividing MediumMultiplying Digit Strings Medium
loading Python…
Input
values=[2, 3, 4]
Expected
6

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