-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountGoodTriplets.java
58 lines (52 loc) · 1.82 KB
/
CountGoodTriplets.java
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
package Algorithms.IntegerArray;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 06 April 2025
*/
public class CountGoodTriplets {
public static void main(String[] args) {
int[] nums = {3,0,1,1,9,7};
int a = 3, b = 4, c = 4;
System.out.println(countGoodTriplets(nums, a, b, c)); // Output: 10
}
public static int countGoodTriplets(int[] nums, int a, int b, int c) {
int count = 0;
for (int i = 0; i < nums.length - 2; i++) {
for (int j = i + 1; j < nums.length - 1; j++) {
if (Math.abs(nums[i] - nums[j]) <= a) {
for (int k = j + 1; k < nums.length; k++) {
if (Math.abs(nums[j] - nums[k]) <= b && Math.abs(nums[i] - nums[k]) <= c) {
count++;
}
}
}
}
}
return count;
}
public int countGoodTriplets2(int[] arr, int a, int b, int c) {
int ans = 0, n = arr.length;
int[] sum = new int[1001];
for (int j = 0; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
if (Math.abs(arr[j] - arr[k]) <= b) {
int lj = arr[j] - a, rj = arr[j] + a;
int lk = arr[k] - c, rk = arr[k] + c;
int l = Math.max(0, Math.max(lj, lk)),
r = Math.min(1000,Math.min(rj, rk));
if (l <= r) {
if (l == 0) {
ans += sum[r];
} else {
ans += sum[r] - sum[l - 1];
}
}
}
}
for (int k = arr[j]; k <= 1000; ++k) {
++sum[k];
}
}
return ans;
}
}