Given a 0-indexed integer array nums and an integer d, return the number of triplets(i, j, k)such thati < j < kand(nums[i] + nums[j] + nums[k]) % d == 0.
Input: nums =[3,3,4,7,8], d =5Output: 3Explanation: The triplets which are divisible by 5 are:(0,1,2),(0,2,4),(1,2,4).It can be shown that no other triplet is divisible by 5. Hence, the answer is3.
Example 2:
1
2
3
Input: nums =[3,3,3,3], d =3Output: 4Explanation: Any triplet chosen here has a sum of 9, which is divisible by 3. Hence, the answer is the total number of triplets which is4.
Example 3:
1
2
3
Input: nums =[3,3,3,3], d =6Output: 0Explanation: Any triplet chosen here has a sum of 9, which is not divisible by 6. Hence, the answer is0.
For each k, count pairs (i, j) with i < j < k such that (nums[i] + nums[j] + nums[k]) % d == 0. For each k, keep a hash map of pair sums modulo d for all previous i, j.
#include<vector>#include<unordered_map>usingnamespace std;
classSolution {
public:int divisibleTripletSums(vector<int>& nums, int d) {
int n = nums.size(), ans =0;
for (int k =2; k < n; ++k) {
unordered_map<int,int> cnt;
for (int i =0; i < k; ++i)
for (int j = i+1; j < k; ++j)
cnt[(nums[i]+nums[j])%d]++;
int need = (d - nums[k]%d)%d;
ans += cnt[need];
}
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funcdivisibleTripletSums(nums []int, dint) int {
n, ans:= len(nums), 0fork:=2; k < n; k++ {
cnt:=map[int]int{}
fori:=0; i < k; i++ {
forj:=i+1; j < k; j++ {
cnt[(nums[i]+nums[j])%d]++ }
}
need:= (d-nums[k]%d)%dans+=cnt[need]
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.*;
classSolution {
publicintdivisibleTripletSums(int[] nums, int d) {
int n = nums.length, ans = 0;
for (int k = 2; k < n; ++k) {
Map<Integer,Integer> cnt =new HashMap<>();
for (int i = 0; i < k; ++i)
for (int j = i+1; j < k; ++j)
cnt.put((nums[i]+nums[j])%d, cnt.getOrDefault((nums[i]+nums[j])%d,0)+1);
int need = (d - nums[k]%d)%d;
ans += cnt.getOrDefault(need,0);
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution {
fundivisibleTripletSums(nums: IntArray, d: Int): Int {
val n = nums.size
var ans = 0for (k in2 until n) {
val cnt = mutableMapOf<Int,Int>()
for (i in0 until k)
for (j in i+1 until k)
cnt[(nums[i]+nums[j])%d] = cnt.getOrDefault((nums[i]+nums[j])%d,0)+1val need = (d - nums[k]%d)%d
ans += cnt.getOrDefault(need,0)
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
classSolution:
defdivisibleTripletSums(self, nums: list[int], d: int) -> int:
n, ans = len(nums), 0for k in range(2, n):
cnt = {}
for i in range(k):
for j in range(i+1, k):
cnt[(nums[i]+nums[j])%d] = cnt.get((nums[i]+nums[j])%d,0)+1 need = (d - nums[k]%d)%d
ans += cnt.get(need,0)
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use std::collections::HashMap;
impl Solution {
pubfndivisible_triplet_sums(nums: Vec<i32>, d: i32) -> i32 {
let n = nums.len();
letmut ans =0;
for k in2..n {
letmut cnt = HashMap::new();
for i in0..k {
for j in i+1..k {
*cnt.entry((nums[i]+nums[j])%d).or_insert(0) +=1;
}
}
let need = (d - nums[k]%d)%d;
ans +=*cnt.get(&need).unwrap_or(&0);
}
ans
}
}