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.
You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:
i < j < k,nums[j] - nums[i] == diff, andnums[k] - nums[j] == diff.Return the number of unique arithmetic triplets.
Example 1:
Input: nums = [0,1,4,6,7,10], diff = 3 Output: 2 Explanation: (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
Example 2:
Input: nums = [4,5,6,7,8,9], diff = 2 Output: 2 Explanation: (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2. (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
Constraints:
3 <= nums.length <= 2000 <= nums[i] <= 2001 <= diff <= 50nums is strictly increasing.Problem summary: You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met: i < j < k, nums[j] - nums[i] == diff, and nums[k] - nums[j] == diff. Return the number of unique arithmetic triplets.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers
[0,1,4,6,7,10] 3
[4,5,6,7,8,9] 2
two-sum)3sum)number-of-unequal-triplets-in-array)maximum-value-of-an-ordered-triplet-i)minimum-sum-of-mountain-triplets-i)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2367: Number of Arithmetic Triplets
class Solution {
public int arithmeticTriplets(int[] nums, int diff) {
int ans = 0;
int n = nums.length;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
for (int k = j + 1; k < n; ++k) {
if (nums[j] - nums[i] == diff && nums[k] - nums[j] == diff) {
++ans;
}
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2367: Number of Arithmetic Triplets
func arithmeticTriplets(nums []int, diff int) (ans int) {
n := len(nums)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
for k := j + 1; k < n; k++ {
if nums[j]-nums[i] == diff && nums[k]-nums[j] == diff {
ans++
}
}
}
}
return
}
# Accepted solution for LeetCode #2367: Number of Arithmetic Triplets
class Solution:
def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
return sum(b - a == diff and c - b == diff for a, b, c in combinations(nums, 3))
// Accepted solution for LeetCode #2367: Number of Arithmetic Triplets
/**
* [2367] Number of Arithmetic Triplets
*
* You are given a 0-indexed, strictly increasing integer array nums and a positive integer diff. A triplet (i, j, k) is an arithmetic triplet if the following conditions are met:
*
* i < j < k,
* nums[j] - nums[i] == diff, and
* nums[k] - nums[j] == diff.
*
* Return the number of unique arithmetic triplets.
*
* Example 1:
*
* Input: nums = [0,1,4,6,7,10], diff = 3
* Output: 2
* Explanation:
* (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3.
* (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3.
*
* Example 2:
*
* Input: nums = [4,5,6,7,8,9], diff = 2
* Output: 2
* Explanation:
* (0, 2, 4) is an arithmetic triplet because both 8 - 6 == 2 and 6 - 4 == 2.
* (1, 3, 5) is an arithmetic triplet because both 9 - 7 == 2 and 7 - 5 == 2.
*
*
* Constraints:
*
* 3 <= nums.length <= 200
* 0 <= nums[i] <= 200
* 1 <= diff <= 50
* nums is strictly increasing.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-arithmetic-triplets/
// discuss: https://leetcode.com/problems/number-of-arithmetic-triplets/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn arithmetic_triplets(nums: Vec<i32>, diff: i32) -> i32 {
nums.iter().fold(0, |acc, &x| {
if nums.contains(&(x + diff)) && nums.contains(&(x + diff * 2)) {
acc + 1
} else {
acc
}
})
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2367_example_1() {
let nums = vec![0, 1, 4, 6, 7, 10];
let diff = 3;
let result = 2;
assert_eq!(Solution::arithmetic_triplets(nums, diff), result);
}
#[test]
fn test_2367_example_2() {
let nums = vec![4, 5, 6, 7, 8, 9];
let diff = 2;
let result = 2;
assert_eq!(Solution::arithmetic_triplets(nums, diff), result);
}
}
// Accepted solution for LeetCode #2367: Number of Arithmetic Triplets
function arithmeticTriplets(nums: number[], diff: number): number {
const n = nums.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 (nums[j] - nums[i] === diff && nums[k] - nums[j] === diff) {
++ans;
}
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
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.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.