1534. Count Good Triplets ¶ Time: $O(n^3)$ Space: $O(1)$ C++JavaPython 1 2 3 4 5 6 7 8 9 10 11 12 13 14class Solution { public: int countGoodTriplets(vector<int>& arr, int a, int b, int c) { int ans = 0; for (int i = 0; i < arr.size(); ++i) for (int j = i + 1; j < arr.size(); ++j) for (int k = j + 1; k < arr.size(); ++k) if (abs(arr[i] - arr[j]) <= a && // abs(arr[j] - arr[k]) <= b && // abs(arr[i] - arr[k]) <= c) ++ans; return ans; } }; 1 2 3 4 5 6 7 8 9 10 11 12 13class Solution { public int countGoodTriplets(int[] arr, int a, int b, int c) { int ans = 0; for (int i = 0; i < arr.length; ++i) for (int j = i + 1; j < arr.length; ++j) for (int k = j + 1; k < arr.length; ++k) if (Math.abs(arr[i] - arr[j]) <= a && // Math.abs(arr[j] - arr[k]) <= b && // Math.abs(arr[i] - arr[k]) <= c) ++ans; return ans; } } 1 2 3 4 5 6 7 8class Solution: def countGoodTriplets(self, arr: list[int], a: int, b: int, c: int) -> int: return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c for i in range(len(arr)) for j in range(i + 1, len(arr)) for k in range(j + 1, len(arr)))