-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountGoodTriplets.java
39 lines (34 loc) · 1.11 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
package com.smlnskgmail.jaman.leetcodejava.easy;
// https://leetcode.com/problems/count-good-triplets/
public class CountGoodTriplets {
private final int[] arr;
private final int a;
private final int b;
private final int c;
public CountGoodTriplets(int[] arr, int a, int b, int c) {
this.arr = arr;
this.a = a;
this.b = b;
this.c = c;
}
public int solution() {
int result = 0;
for (int i = 0; i < arr.length - 2; i++) {
int first = arr[i];
for (int j = i + 1; j < arr.length; j++) {
int second = arr[j];
if (Math.abs(first - second) <= a) {
if (Math.abs(first - second) <= a) {
for (int k = j + 1; k < arr.length; k++) {
int third = arr[k];
if (Math.abs(second - third) <= b && Math.abs(first - third) <= c) {
result++;
}
}
}
}
}
}
return result;
}
}