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.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array.
A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true:
0 <= i, j < nums.lengthi != j|nums[i] - nums[j]| == kNotice that |val| denotes the absolute value of val.
Example 1:
Input: nums = [3,1,4,1,5], k = 2 Output: 2 Explanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5). Although we have two 1s in the input, we should only return the number of unique pairs.
Example 2:
Input: nums = [1,2,3,4,5], k = 1 Output: 4 Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).
Example 3:
Input: nums = [1,3,1,5,4], k = 0 Output: 1 Explanation: There is one 0-diff pair in the array, (1, 1).
Constraints:
1 <= nums.length <= 104-107 <= nums[i] <= 1070 <= k <= 107Problem summary: Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array. A k-diff pair is an integer pair (nums[i], nums[j]), where the following are true: 0 <= i, j < nums.length i != j |nums[i] - nums[j]| == k Notice that |val| denotes the absolute value of val.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Two Pointers · Binary Search
[3,1,4,1,5] 2
[1,2,3,4,5] 1
[1,3,1,5,4] 0
minimum-absolute-difference-in-bst)count-number-of-pairs-with-absolute-difference-k)kth-smallest-product-of-two-sorted-arrays)count-number-of-bad-pairs)number-of-pairs-satisfying-inequality)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #532: K-diff Pairs in an Array
class Solution {
public int findPairs(int[] nums, int k) {
Set<Integer> ans = new HashSet<>();
Set<Integer> vis = new HashSet<>();
for (int x : nums) {
if (vis.contains(x - k)) {
ans.add(x - k);
}
if (vis.contains(x + k)) {
ans.add(x);
}
vis.add(x);
}
return ans.size();
}
}
// Accepted solution for LeetCode #532: K-diff Pairs in an Array
func findPairs(nums []int, k int) int {
ans := make(map[int]struct{})
vis := make(map[int]struct{})
for _, x := range nums {
if _, ok := vis[x-k]; ok {
ans[x-k] = struct{}{}
}
if _, ok := vis[x+k]; ok {
ans[x] = struct{}{}
}
vis[x] = struct{}{}
}
return len(ans)
}
# Accepted solution for LeetCode #532: K-diff Pairs in an Array
class Solution:
def findPairs(self, nums: List[int], k: int) -> int:
ans = set()
vis = set()
for x in nums:
if x - k in vis:
ans.add(x - k)
if x + k in vis:
ans.add(x)
vis.add(x)
return len(ans)
// Accepted solution for LeetCode #532: K-diff Pairs in an Array
use std::collections::HashSet;
impl Solution {
pub fn find_pairs(nums: Vec<i32>, k: i32) -> i32 {
let mut ans = HashSet::new();
let mut vis = HashSet::new();
for &x in &nums {
if vis.contains(&(x - k)) {
ans.insert(x - k);
}
if vis.contains(&(x + k)) {
ans.insert(x);
}
vis.insert(x);
}
ans.len() as i32
}
}
// Accepted solution for LeetCode #532: K-diff Pairs in an Array
function findPairs(nums: number[], k: number): number {
const ans = new Set<number>();
const vis = new Set<number>();
for (const x of nums) {
if (vis.has(x - k)) {
ans.add(x - k);
}
if (vis.has(x + k)) {
ans.add(x);
}
vis.add(x);
}
return ans.size;
}
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.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.