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 byd
Return the number of such divisible triplet sums.
Example 1
Input
nums = [3,3,4,7,8], d = 5Output
3The valid triplets are
(0, 1, 2), (0, 2, 4), and (1, 2, 4), whose sums are divisible by 5.Example 2
Input
nums = [3,3,3,3], d = 3Output
4Every 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