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 integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
The value of |x| is defined as:
x if x >= 0.-x if x < 0.Example 1:
Input: nums = [1,2,2,1], k = 1 Output: 4 Explanation: The pairs with an absolute difference of 1 are: - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] - [1,2,2,1]
Example 2:
Input: nums = [1,3], k = 3 Output: 0 Explanation: There are no pairs with an absolute difference of 3.
Example 3:
Input: nums = [3,2,1,5,4], k = 2 Output: 3 Explanation: The pairs with an absolute difference of 2 are: - [3,2,1,5,4] - [3,2,1,5,4] - [3,2,1,5,4]
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= 1001 <= k <= 99Problem summary: Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k. The value of |x| is defined as: x if x >= 0. -x if x < 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,2,1] 1
[1,3] 3
[3,2,1,5,4] 2
two-sum)k-diff-pairs-in-an-array)finding-pairs-with-a-certain-sum)count-equal-and-divisible-pairs-in-an-array)count-number-of-bad-pairs)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2006: Count Number of Pairs With Absolute Difference K
class Solution {
public int countKDifference(int[] nums, int k) {
int ans = 0;
for (int i = 0, n = nums.length; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (Math.abs(nums[i] - nums[j]) == k) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2006: Count Number of Pairs With Absolute Difference K
func countKDifference(nums []int, k int) int {
n := len(nums)
ans := 0
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
if abs(nums[i]-nums[j]) == k {
ans++
}
}
}
return ans
}
func abs(x int) int {
if x > 0 {
return x
}
return -x
}
# Accepted solution for LeetCode #2006: Count Number of Pairs With Absolute Difference K
class Solution:
def countKDifference(self, nums: List[int], k: int) -> int:
n = len(nums)
return sum(
abs(nums[i] - nums[j]) == k for i in range(n) for j in range(i + 1, n)
)
// Accepted solution for LeetCode #2006: Count Number of Pairs With Absolute Difference K
impl Solution {
pub fn count_k_difference(nums: Vec<i32>, k: i32) -> i32 {
let mut res = 0;
let n = nums.len();
for i in 0..n - 1 {
for j in i..n {
if (nums[i] - nums[j]).abs() == k {
res += 1;
}
}
}
res
}
}
// Accepted solution for LeetCode #2006: Count Number of Pairs With Absolute Difference K
function countKDifference(nums: number[], k: number): number {
let ans = 0;
let cnt = new Map();
for (let num of nums) {
ans += (cnt.get(num - k) || 0) + (cnt.get(num + k) || 0);
cnt.set(num, (cnt.get(num) || 0) + 1);
}
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.
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.