Leetcode 532: K-diff Pairs in an Array
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 a pair of integers (nums[i], nums[j]) where the absolute difference between the values of the pair is k, and i != j.
Problem
Approach
Steps
Complexity
Input: The input consists of an array of integers nums and an integer k. The array can have up to 10^4 elements, and k is a non-negative integer.
Example: Input: nums = [5, 1, 6, 1, 8], k = 3
Constraints:
• 1 <= nums.length <= 10^4
• -10^7 <= nums[i] <= 10^7
• 0 <= k <= 10^7
Output: Return the number of unique k-diff pairs in the array.
Example: Output: 2
Constraints:
• The output should be the number of unique pairs with the specified absolute difference.
Goal: To find all unique pairs (i, j) such that the absolute difference between nums[i] and nums[j] equals k.
Steps:
• Count the occurrences of each number in the array using a hashmap.
• For each number in the hashmap, check if a pair exists that satisfies the absolute difference of k.
• If k is greater than 0, check if nums[i] - k exists in the map.
• If k is equal to 0, check if a number appears more than once in the map.
Goal: The problem has constraints that ensure the array is not empty and the values are within the specified ranges.
Steps:
• The number of elements in the array will be between 1 and 10^4.
• Each element of the array will be in the range [-10^7, 10^7].
• The integer k will be in the range [0, 10^7].
Assumptions:
• The array contains at least one number.
• The input value k is a valid non-negative integer.
• Input: Input: nums = [5, 1, 6, 1, 8], k = 3
• Explanation: There are two pairs with a difference of 3: (1, 4) and (5, 8). Since the pairs are unique, the output is 2.
Approach: To solve this problem efficiently, we will use a hashmap to store the frequency of each element in the array and check for the existence of the required pairs.
Observations:
• Using a hashmap to count occurrences of numbers allows for efficient lookups when checking for pairs.
• If k is 0, we need to check for duplicates, while for k > 0, we need to find numbers that differ by k.
Steps:
• Create a hashmap to store the frequency of each number in the array.
• For each element in the hashmap, check if a valid pair exists based on the value of k.
• If k > 0, check if nums[i] - k exists in the map. If k == 0, ensure the frequency of the number is greater than 1.
Empty Inputs:
• If the input array is empty, return 0.
Large Inputs:
• Handle cases with the maximum number of elements efficiently using a hashmap.
Special Values:
• Consider the case when k = 0, where the only valid pairs are those that have duplicates.
Constraints:
• Ensure that pairs are counted uniquely and that duplicates are handled correctly.
int findPairs(vector<int>& nums, int k) {
map<int, int> mp;
for(int i : nums)
mp[i]++;
int res = 0;
for(auto const &[i, j] : mp)
if((k > 0 && mp.count(i - k)) ||
(k == 0 && (j > 1)) )
res++;
return res;
}
1 : Function Definition, Hash Map Usage
int findPairs(vector<int>& nums, int k) {
Defines the `findPairs` function, which takes an integer array `nums` and an integer `k` as input and returns the number of unique pairs with the absolute difference equal to `k`.
2 : Map Initialization
map<int, int> mp;
Initializes a hash map `mp` where the keys are the elements of the array `nums`, and the values are the frequency of those elements.
3 : Loop, Array Traversal
for(int i : nums)
Iterates through each element `i` in the array `nums`.
4 : Count Elements
mp[i]++;
Increments the count of the current element `i` in the map `mp`. This stores the frequency of each element in the array.
5 : Initialize Result
int res = 0;
Initializes the variable `res` to store the result, i.e., the number of pairs with the required absolute difference.
6 : Iterate over Map
for(auto const &[i, j] : mp)
Iterates through the map `mp`, where `i` is the key (the element of the array) and `j` is the value (the frequency of that element).
7 : Condition for k > 0
if((k > 0 && mp.count(i - k)) ||
Checks if `k > 0` and if the map contains an element `i - k` (the complement of the current element `i` that would form a pair with the absolute difference `k`).
8 : Condition for k == 0
(k == 0 && (j > 1)) )
Checks if `k == 0` and the frequency of the current element `i` is greater than 1, indicating that there is at least one duplicate, forming a valid pair.
9 : Increment Result
res++;
Increments the result `res` if the conditions for a valid pair are met.
10 : Return Result
return res;
Returns the final count of valid pairs.
Best Case: O(n)
Average Case: O(n)
Worst Case: O(n)
Description: The time complexity is O(n), where n is the number of elements in the input array. This is due to the hashmap creation and lookups.
Best Case: O(n)
Worst Case: O(n)
Description: The space complexity is O(n), as we use a hashmap to store the frequency of each element in the array.
LeetCode Solutions Library / DSA Sheets / Course Catalog |
---|
comments powered by Disqus