-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2964-NumberOfDivisibleTripletSums.go
More file actions
90 lines (78 loc) · 3 KB
/
2964-NumberOfDivisibleTripletSums.go
File metadata and controls
90 lines (78 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
// 2964. Number of Divisible Triplet Sums
// Given a 0-indexed integer array nums and an integer d,
// return the number of triplets (i, j, k) such that i < j < k and (nums[i] + nums[j] + nums[k]) % d == 0.
// Example 1:
// Input: nums = [3,3,4,7,8], d = 5
// Output: 3
// Explanation: 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 is 3.
// Example 2:
// Input: nums = [3,3,3,3], d = 3
// Output: 4
// Explanation: Any triplet chosen here has a sum of 9, which is divisible by 3. Hence, the answer is the total number of triplets which is 4.
// Example 3:
// Input: nums = [3,3,3,3], d = 6
// Output: 0
// Explanation: Any triplet chosen here has a sum of 9, which is not divisible by 6. Hence, the answer is 0.
// Constraints:
// 1 <= nums.length <= 1000
// 1 <= nums[i] <= 10^9
// 1 <= d <= 10^9
import "fmt"
func divisibleTripletCount(nums []int, d int) int {
res, n := 0, len(nums)
mp := make(map[int]int)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
res += mp[(d - (nums[i] + nums[j]) % d) % d]
}
mp[nums[i] % d]++
}
return res
}
func divisibleTripletCount1(nums []int, d int) int {
res, n := 0, len(nums)
count := make([]int, d)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
res += count[(d - (nums[i] + nums[j]) % d) % d]
}
count[nums[i] % d]++
}
return res
}
func divisibleTripletCount2(nums []int, d int) int {
res, count := 0, make([]int, d)
for i, v := range nums {
res += count[(d - v % d) % d]
for j := 0; j < i; j++ {
count[(nums[j] + nums[i]) % d]++
}
}
return res
}
func main() {
// Example 1:
// Input: nums = [3,3,4,7,8], d = 5
// Output: 3
// Explanation: 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 is 3.
fmt.Println(divisibleTripletCount([]int{3,3,4,7,8}, 5)) // 3
// Example 2:
// Input: nums = [3,3,3,3], d = 3
// Output: 4
// Explanation: Any triplet chosen here has a sum of 9, which is divisible by 3. Hence, the answer is the total number of triplets which is 4.
fmt.Println(divisibleTripletCount([]int{3,3,3,3}, 3)) // 4
// Example 3:
// Input: nums = [3,3,3,3], d = 6
// Output: 0
// Explanation: Any triplet chosen here has a sum of 9, which is not divisible by 6. Hence, the answer is 0.
fmt.Println(divisibleTripletCount([]int{3,3,3,3}, 6)) // 0
fmt.Println(divisibleTripletCount1([]int{3,3,4,7,8}, 5)) // 3
fmt.Println(divisibleTripletCount1([]int{3,3,3,3}, 3)) // 4
fmt.Println(divisibleTripletCount1([]int{3,3,3,3}, 6)) // 0
fmt.Println(divisibleTripletCount2([]int{3,3,4,7,8}, 5)) // 3
fmt.Println(divisibleTripletCount2([]int{3,3,3,3}, 3)) // 4
fmt.Println(divisibleTripletCount2([]int{3,3,3,3}, 6)) // 0
}