532. K-diff Pairs in an Array - Detailed Explanation
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 valuexfor whichx + kalso appears.
Approaches
Approach 1: HashSet / Frequency Map (O(n) time, O(n) space)
- If
k < 0, immediately return 0. - Build a frequency map
freqof all values innums. - If
k == 0, count how many values havefreq[v] ≥ 2. - If
k > 0, for each unique valuevinfreq, check ifv + kexists; count one for each match. - Return the count.
Why it works:
- For
k > 0, each distinctvforms 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
iandj = 1. - While
i < nums.lengthandj < nums.length:- If
i == jornums[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
iandjpast 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
numsof 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
3428. Maximum and Minimum Sums of at Most Size K Subsequences - Detailed Explanation
Learn to solve Leetcode 3428. Maximum and Minimum Sums of at Most Size K Subsequences with multiple approaches.
977. Squares of a Sorted Array - Detailed Explanation
Learn to solve Leetcode 977. Squares of a Sorted Array with multiple approaches.
584. Find Customer Referee - Detailed Explanation
Learn to solve Leetcode 584. Find Customer Referee with multiple approaches.
468. Validate IP Address - Detailed Explanation
Learn to solve Leetcode 468. Validate IP Address with multiple approaches.
912. Sort an Array - Detailed Explanation
Learn to solve Leetcode 912. Sort an Array with multiple approaches.
317. Shortest Distance from All Buildings - Detailed Explanation
Learn to solve Leetcode 317. Shortest Distance from All Buildings with multiple approaches.
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.
4.6
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.