K-diff Pairs in an Array - Complete Solution Guide
K-diff Pairs in an Array is LeetCode problem 532, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 . 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 o
Detailed Explanation
The problem asks us to find the number of unique k-diff pairs in a given array `nums`. A k-diff pair `(nums[i], nums[j])` satisfies the following conditions: `0 <= i, j < nums.length`, `i != j`, and `|nums[i] - nums[j]| == k`. We need to count how many such pairs exist, ensuring that we only count unique pairs, even if numbers appear multiple times in the input array. The input is an array of integers `nums` and an integer `k` (the difference). The output is the number of unique k-diff pairs.
Solution Approach
The solution uses a hash table (or Counter in Python) to store the counts of each number in the input array. Then, it iterates through the hash table. If k is 0, it counts the number of elements with a count greater than 1 (meaning there are pairs of the same element). If k is greater than 0, it checks if the number plus k exists in the hash table and if so, increments the count.
Step-by-Step Algorithm
- Step 1: Initialize a hash table (or Counter) to store the frequency of each number in the input array `nums`.
- Step 2: Handle the edge case where `k` is negative. If `k < 0`, return 0 because the absolute difference cannot be negative.
- Step 3: Check if `k` is equal to 0. If it is, iterate through the hash table's values (counts). For each value, if the count is greater than 1, increment the `pair_count`.
- Step 4: If `k` is not equal to 0, iterate through the keys (numbers) in the hash table. For each number, check if `number + k` exists as a key in the hash table. If it does, increment the `pair_count`.
- Step 5: Return the final `pair_count`.
Key Insights
- Insight 1: The absolute difference `|nums[i] - nums[j]| == k` implies that `nums[i] - nums[j] == k` or `nums[j] - nums[i] == k`. Therefore, we are looking for pairs where one number is `k` more or `k` less than the other.
- Insight 2: A hash table (or Counter in Python) is an efficient data structure to count the frequency of each number in the array. This helps in identifying and counting unique pairs.
- Insight 3: When k=0, we are looking for pairs of identical numbers. In this special case, we only need to count numbers that appear more than once in the array. When k>0, we need to check if `num + k` exists in the array.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Two Pointers, Binary Search, Sorting.
Companies
Asked at: Capital One, tcs.