Skip to content

Commit 8781907

Browse files
authored
Create count-good-triplets.cpp
1 parent 21de1af commit 8781907

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

C++/count-good-triplets.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n^3)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
7+
int result = 0;
8+
for (int i = 0; i + 2 < arr.size(); ++i) {
9+
for (int j = i + 1; j + 1 < arr.size(); ++j) {
10+
for (int k = j + 1; k < arr.size(); ++k) {
11+
if (abs(arr[i] - arr[j]) <= a &&
12+
abs(arr[j] - arr[k]) <= b &&
13+
abs(arr[k] - arr[i]) <= c) {
14+
++result;
15+
}
16+
}
17+
}
18+
}
19+
return result;
20+
}
21+
};

0 commit comments

Comments
 (0)