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 <= 107When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The most straightforward way to solve this is to systematically create every possible pair of numbers from the list. We then check each of these pairs to see if their difference matches the required value.
Here's how the algorithm would work step-by-step:
def count_k_diff_pairs_brute_force(collection_of_numbers, target_difference):
# We use a set to store found pairs, which automatically handles uniqueness.
unique_valid_pairs = set()
for first_number_index in range(len(collection_of_numbers)):
# To avoid redundant comparisons, we only check pairs where the second number appears after the first.
for second_number_index in range(first_number_index + 1, len(collection_of_numbers)):
first_number = collection_of_numbers[first_number_index]
second_number = collection_of_numbers[second_number_index]
if abs(first_number - second_number) == target_difference:
# Sorting the pair ensures (a, b) and (b, a) are treated as one unique pair.
canonical_pair_representation = tuple(sorted((first_number, second_number)))
unique_valid_pairs.add(canonical_pair_representation)
return len(unique_valid_pairs)The optimal approach avoids a slow, brute-force check of every possible pair. Instead, it first creates a quick count of all the numbers in the list, which allows us to then simply look up if a number's required partner—the one that makes the target difference—exists.
Here's how the algorithm would work step-by-step:
def findPairs(numbers_list, required_difference):
# A negative difference is impossible, so we can immediately return zero.
if required_difference < 0:
return 0
# A zero difference requires a special case to count duplicate numbers.
if required_difference == 0:
number_counts = {}
for number in numbers_list:
number_counts[number] = number_counts.get(number, 0) + 1
pair_count = 0
for count in number_counts.values():
if count > 1:
pair_count += 1
return pair_count
# For positive differences, a set is efficient for finding unique numbers and their partners.
unique_numbers_roster = set(numbers_list)
pair_count = 0
for number in unique_numbers_roster:
required_partner = number + required_difference
if required_partner in unique_numbers_roster:
pair_count += 1
return pair_count| Case | How to Handle |
|---|---|
| The difference k is zero | This case requires special logic to count the number of unique elements that appear more than once in the array. |
| Input array contains duplicate numbers with k > 0 | The solution must count pairs of values only once, for example by processing only unique numbers from the input. |
| The input array size is at its maximum limit (10^4) | An efficient approach with O(N) or O(N log N) time complexity is necessary to avoid a timeout. |
| Input numbers span a very wide range (e.g., -10^7 to 10^7) | Using a hash map is memory-efficient compared to a direct-addressing array which would be too large. |
| All array elements are identical | The solution should correctly return 1 if k is 0 and 0 for any k > 0. |
| Input array contains only one element | The algorithm must return 0 since no pair can be formed. |
| No pairs with the specified difference k exist | The algorithm should complete its search and correctly return a final count of 0. |
| Input contains negative numbers and zero | The chosen hash-based or sorting-based algorithm should handle negative numbers correctly without special casing. |