Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets.
A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true:
0 <= i < j < k < arr.length|arr[i] - arr[j]| <= a|arr[j] - arr[k]| <= b|arr[i] - arr[k]| <= cWhere |x| denotes the absolute value of x.
Return the number of good triplets.
Example 1:
Input: arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3 Output: 4 Explanation: There are 4 good triplets: [(3,0,1), (3,0,1), (3,1,1), (0,1,1)].
Example 2:
Input: arr = [1,1,2,2,3], a = 0, b = 0, c = 1 Output: 0 Explanation: No triplet satisfies all conditions.
Constraints:
3 <= arr.length <= 1000 <= arr[i] <= 10000 <= a, b, c <= 1000Problem summary: Given an array of integers arr, and three integers a, b and c. You need to find the number of good triplets. A triplet (arr[i], arr[j], arr[k]) is good if the following conditions are true: 0 <= i < j < k < arr.length |arr[i] - arr[j]| <= a |arr[j] - arr[k]| <= b |arr[i] - arr[k]| <= c Where |x| denotes the absolute value of x. Return the number of good triplets.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[3,0,1,1,9,7] 7 2 3
[1,1,2,2,3] 0 0 1
count-special-quadruplets)number-of-unequal-triplets-in-array)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1534: Count Good Triplets
class Solution {
public int countGoodTriplets(int[] arr, int a, int b, int c) {
int n = arr.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++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;
}
}
// Accepted solution for LeetCode #1534: Count Good Triplets
func countGoodTriplets(arr []int, a int, b int, c int) (ans int) {
n := len(arr)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for k := j + 1; k < n; k++ {
if abs(arr[i]-arr[j]) <= a && abs(arr[j]-arr[k]) <= b && abs(arr[i]-arr[k]) <= c {
ans++
}
}
}
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
# Accepted solution for LeetCode #1534: Count Good Triplets
class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
ans, n = 0, len(arr)
for i in range(n):
for j in range(i + 1, n):
for k in range(j + 1, n):
ans += (
abs(arr[i] - arr[j]) <= a
and abs(arr[j] - arr[k]) <= b
and abs(arr[i] - arr[k]) <= c
)
return ans
// Accepted solution for LeetCode #1534: Count Good Triplets
impl Solution {
pub fn count_good_triplets(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
let n = arr.len();
let mut ans = 0;
for i in 0..n {
for j in i + 1..n {
for k in j + 1..n {
if (arr[i] - arr[j]).abs() <= a
&& (arr[j] - arr[k]).abs() <= b
&& (arr[i] - arr[k]).abs() <= c
{
ans += 1;
}
}
}
}
ans
}
}
// Accepted solution for LeetCode #1534: Count Good Triplets
function countGoodTriplets(arr: number[], a: number, b: number, c: number): number {
let n = arr.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = i + 1; j < n; ++j) {
for (let k = j + 1; k < n; ++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;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.