Count Good Triplets (LeetCode 1534): Solution and Explanation
Count index triplets i < j < k whose three pairwise value gaps stay within the limits a, b and c. With n at most 100, enumerating triplets with one early skip is the intended solution.
Intuition
Think of the three chosen positions as the corners of a triangle. Each side has its own limit: the i-j side must be at most a, the j-k side at most b and the i-k side at most c. A triplet is good only when all three sides pass.
There are at most C(100, 3), about 160,000, triplets, which is small enough to check one by one.
The i-j side does not involve k at all. If it already fails, every k for that pair fails too, so the k loop can be skipped. That single check removes most of the work when a is tight.
Approach: Enumerate with pruning
- Set count = 0.
- For every pair i < j, compute |arr[i] - arr[j]|. If it is greater than a, move on to the next j.
- Otherwise, for every k > j, check |arr[j] - arr[k]| <= b and |arr[i] - arr[k]| <= c. Add one to count when both hold.
- Return count.
Step-by-step example
arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3
- i = 0 (3), j = 1 (0): gap 3 <= 7. k = 2 (1) and k = 3 (1) pass both other checks, count = 2. k = 4 (9) and k = 5 (7) fail the j-k limit.
- i = 0, j = 2 (1): gap 2 <= 7. k = 3 (1) passes, count = 3.
- i = 0, j = 4 (9) and j = 5 (7): the j-k or i-k limits fail for every k.
- i = 1 (0), j = 2 (1): k = 3 (1) passes, count = 4.
- No other triplet passes all three checks, so the answer is 4.
Code: Python, Java, C++
1class Solution:
2 def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
3 n = len(arr)
4 count = 0
5 for i in range(n):
6 for j in range(i + 1, n):
7 if abs(arr[i] - arr[j]) > a:
8 continue
9 for k in range(j + 1, n):
10 if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
11 count += 1
12 return count1class Solution {
2 public int countGoodTriplets(int[] arr, int a, int b, int c) {
3 int n = arr.length;
4 int count = 0;
5 for (int i = 0; i < n; i++) {
6 for (int j = i + 1; j < n; j++) {
7 if (Math.abs(arr[i] - arr[j]) > a) continue;
8 for (int k = j + 1; k < n; k++) {
9 if (Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) {
10 count++;
11 }
12 }
13 }
14 }
15 return count;
16 }
17}1class Solution {
2public:
3 int countGoodTriplets(vector<int>& arr, int a, int b, int c) {
4 int n = arr.size();
5 int count = 0;
6 for (int i = 0; i < n; i++) {
7 for (int j = i + 1; j < n; j++) {
8 if (abs(arr[i] - arr[j]) > a) continue;
9 for (int k = j + 1; k < n; k++) {
10 if (abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c) {
11 count++;
12 }
13 }
14 }
15 }
16 return count;
17 }
18};Time and space complexity
O(n^3)
Three nested loops over i < j < k, at most n^3 / 6 triplets. The pruning helps in practice but not in the worst case.
O(1)
Only loop indices and the counter are stored.
Other approaches
Fix j and k, count i with a prefix sum (time O(n^2 + n * V), space O(V))
For each pair j < k that passes the b check, the valid arr[i] values form the intersection of [arr[j] - a, arr[j] + a] and [arr[k] - c, arr[k] + c]. Keep a prefix count of the values seen before j over the range 0..1000 and read off how many fall in that interval.
Common mistakes
- Letting indices repeat or go out of order. The loops must start at i + 1 and j + 1.
- Mixing up which limit belongs to which pair: a is for i and j, b for j and k, c for i and k.
- Counting values instead of index triplets. Equal values at different indices are different triplets, which is why (3,0,1) is counted twice in Example 1.
Frequently asked questions
Is O(n^3) fast enough?
Yes. With n <= 100 there are at most about 160,000 triplets, which runs in milliseconds.
Why can the k loop be skipped when the i-j gap is too large?
The i-j condition does not depend on k. If it fails, it fails for every k, so none of those triplets can be good.
Do duplicate values count separately?
Yes. The question counts index triplets, so the same values at different positions are separate triplets.