Mid/Senior

Number of Divisible Triplet Sums

Given a 0-indexed integer array nums and an integer d, count the number of triplets of indices (i, j, k) such that:

  • 0 <= i < j < k < nums.length
  • (nums[i] + nums[j] + nums[k]) is divisible by d

Return the number of such divisible triplet sums.

Example 1
Inputnums = [3,3,4,7,8], d = 5
Output3
The valid triplets are (0, 1, 2), (0, 2, 4), and (1, 2, 4), whose sums are divisible by 5.
Example 2
Inputnums = [3,3,3,3], d = 3
Output4
Every choice of three indices has sum 9, which is divisible by 3, so all 4 triplets are valid.

Constraints

  • 3 <= nums.length <= 1000
  • 1 <= nums[i] <= 10^9
  • 1 <= d <= 10^9

Asked at 8 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate