All problems
0561MediumArrayHash Table

Triple Readings That Split Evenly

Tracked in this browser only
Write code

Trains the technique from

LeetCode 2964Number of Divisible Triplet Sums

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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