Number of Divisible Triplet Sums
Real-world version: Triple Readings That Split EvenlyThe statement here is our own: the same inputs, outputs and technique.
A monitoring rig stores one reading per channel in the array nums, and a reporting tool batches readings into groups whose total has to divide evenly among d shifts.
Count the position triples (i, j, k) with i < j < k for which nums[i] + nums[j] + nums[k] is a multiple of d. Two triples are the same triple when they use the same three positions, so each set of three positions is counted once even if the readings there are equal.
Return that count.
Examples
Example 1
- Input
- nums = [9, 4, 7, 8, 6, 12], d = 5
- Output
- 3
Three position triples qualify: (0, 1, 2) totals 20, (0, 1, 5) totals 25 and (2, 4, 5) totals 25, and each of those is a multiple of 5.
Example 2
- Input
- nums = [2, 2, 2, 2], d = 3
- Output
- 4
Every triple of positions totals 6, which is a multiple of 3, and there are 4 ways to choose three of the four positions.
Example 3
- Input
- nums = [6, 9], d = 4
- Output
- 0
Fewer than three readings exist, so no triple can be formed.
Constraints
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 10^9
- 1 <= d <= 10^9
- With at most 1000 readings the count never exceeds 166167000.
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def divisible_triplet_count(nums: list[int], d: int) -> int:public int divisibleTripletCount(int[] nums, int d)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 12 test cases: 12 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's