272. Number of Divisible Triplet Sums
Given a 0-indexed integer array nums and an integer d, the task is to determine the number of triplets (i, j, k) such that i < j < k and the sum of the triplet is divisible by d, i.e., (nums[i]+nums[j]+nums[k])%d=0
Return the count of such valid triplets.
Example 1:
Input: nums = [2, 5, 7, 9, 10], d = 4
Output: 3
Explanation:
The valid triplets satisfying the condition are: (0, 1, 3), (1, 2, 4), (1, 3, 4).
Thus, the answer is 3.
Example 2:
Input: nums = [4, 4, 4, 4], d = 4
Output: 4
Explanation:
Every triplet chosen from the array sums to 12, which is divisible by 4.
The total number of possible triplets is 4.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [5, 10, 15, 20, 25], d = 5
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 1000
- 1 <= nums[i] <= 109
- 1 <= d <= 109