532. K-diff Pairs in an Array - Detailed Explanation
Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!
Problem Statement
Given an integer array nums
and an integer k
, a k-diff pair is a pair of integers (nums[i], nums[j])
such that:
0 ≤ i < j < nums.length
|nums[i] - nums[j]| == k
Return the number of unique k-diff pairs in the array. Two pairs (a,b)
and (c,d)
are considered the same if they contain the same two values, regardless of order or indices.
Examples
Input: nums = [3,1,4,1,5], k = 2
Output: 2
Explanation: The unique pairs are (1,3) and (3,5).
Input: nums = [1,2,3,4,5], k = 1
Output: 4
Explanation: Pairs are (1,2),(2,3),(3,4),(4,5).
Input: nums = [1,3,1,5,4], k = 0
Output: 1
Explanation: Only (1,1) is valid since 1 appears at least twice.
Constraints
1 ≤ nums.length ≤ 10⁴
-10⁷ ≤ nums[i] ≤ 10⁷
0 ≤ k ≤ 10⁷
Hints
- If
k < 0
, no pairs can satisfy|a - b| = k
. - When
k == 0
, you’re looking for values that appear at least twice. - When
k > 0
, you want to count each valuex
for whichx + k
also appears.
Approaches
Approach 1: HashSet / Frequency Map (O(n) time, O(n) space)
- If
k < 0
, immediately return 0. - Build a frequency map
freq
of all values innums
. - If
k == 0
, count how many values havefreq[v] ≥ 2
. - If
k > 0
, for each unique valuev
infreq
, check ifv + k
exists; count one for each match. - Return the count.
Why it works:
- For
k > 0
, each distinctv
forms exactly one pair(v, v+k)
. - For
k == 0
, each value with frequency ≥2 yields exactly one distinct pair(v, v)
.
Approach 2: Two‑Pointer on Sorted Array (O(n log n) time, O(1) extra space)
- Sort
nums
. - Use two pointers
i
andj = 1
. - While
i < nums.length
andj < nums.length
:- If
i == j
ornums[j] - nums[i] < k
, incrementj
. - Else if
nums[j] - nums[i] > k
, incrementi
. - Else (
nums[j] - nums[i] == k
):- Count one unique pair; increment both
i
andj
past any duplicates to avoid recounting.
- Count one unique pair; increment both
- If
- Return the total count.
Why it works:
- Sorting groups duplicates and lets the difference test run in linear time with two pointers.
- Skipping over duplicates ensures uniqueness.
Step‑by‑Step Walkthrough (HashSet Approach)
nums = [3,1,4,1,5], k = 2
freq = {1:2, 3:1, 4:1, 5:1}
k > 0, so for each v:
v=1 → check 1+2=3 in freq? yes → count=1
v=3 → check 3+2=5? yes → count=2
v=4 → check 6? no
v=5 → check 7? no
Return 2
Complexity Analysis
- Time:
- HashSet approach: O(n) to build freq + O(n) to scan keys → O(n).
- Two‑pointer approach: O(n log n) to sort + O(n) scan → O(n log n).
- Space:
- HashSet: O(n) for the freq map.
- Two‑pointer: O(1) extra (sorting may use O(n) internally).
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Forgetting to handle the special case
k == 0
. - Counting duplicate pairs more than once (e.g., counting
(1,3)
twice if there are multiple 1’s). - Not returning 0 when
k < 0
.
Edge Cases
nums
of length 1 → return 0.- All elements identical and
k == 0
→ one valid pair. - Mixed positive and negative numbers.
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.