Ctrl K

Triples Summing to a Multiple

Medium
You are given an array of positive integers values and a number divisor. Count the triples of indices (i, j, k) with i < j < k for which values[i] + values[j] + values[k] is divisible by divisor. Triples are told apart by their indices: equal values at different positions make different triples.
Example 1
values=[2, 5, 1, 4, 3], divisor=6 → 2

Two triples work: indices (0, 2, 4) with 2 + 1 + 3 = 6, and (1, 3, 4) with 5 + 4 + 3 = 12. None of the other eight sums is divisible by 6.

Example 2
values=[6, 6, 6, 6], divisor=4 → 0

Every triple sums to 18, which is not divisible by 4.

Example 3
values=[5, 10, 15, 20], divisor=5 → 4

Every number is a multiple of 5, so all 4 triples qualify.

Constraints
  • 1 ≤ values.length ≤ 1000
  • 1 ≤ values[i] ≤ 10⁹
  • 1 ≤ divisor ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSudoku Board Without Clashes MediumAnagram Buckets MediumZero Out the Crosses MediumTree From Pre-Order and In-Order Walks MediumTree From In-Order and Post-Order Walks Medium
loading Python…
Input
values=[2, 5, 1, 4, 3], divisor=6
Expected
2

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