Show problems as
Classic problem #2964MediumArrayHash Table

Number of Divisible Triplet Sums

Real-world version: Triple Readings That Split Evenly
Tracked in this browser only
Read Number of Divisible Triplet Sums on LeetCode(opens in a new tab)

The 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.

Python
def divisible_triplet_count(nums: list[int], d: int) -> int:
Java
public int divisibleTripletCount(int[] nums, int d)