Given an integer array nums and an integer k, return the number of pairs (i, j) where i < j such that |nums[i] - nums[j]| == k.
The value of |x| is defined as:
x if x >= 0.-x if x < 0.Example 1:
Input: nums = [1,2,2,1], k = 1 Output: 4 Explanation: The pairs with an absolute difference of 1 are: - [1,2,2,1] - [1,2,2,1] - [1,2,2,1] - [1,2,2,1]
Example 2:
Input: nums = [1,3], k = 3 Output: 0 Explanation: There are no pairs with an absolute difference of 3.
Example 3:
Input: nums = [3,2,1,5,4], k = 2 Output: 3 Explanation: The pairs with an absolute difference of 2 are: - [3,2,1,5,4] - [3,2,1,5,4] - [3,2,1,5,4]
Constraints:
1 <= nums.length <= 2001 <= nums[i] <= 1001 <= k <= 99When 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 brute force way to find pairs with a specific absolute difference involves checking every possible pair. We simply compare each number in the list with every other number. If their absolute difference matches what we are looking for, we count it.
Here's how the algorithm would work step-by-step:
def count_pairs_with_absolute_difference_k(numbers, target_difference):
pair_count = 0
# Iterate through each number in the list
for first_number_index in range(len(numbers)):
# Compare the current number with the rest of the numbers after it.
for second_number_index in range(first_number_index + 1, len(numbers)):
# Get the absolute difference for comparison
absolute_difference = abs(numbers[first_number_index] - numbers[second_number_index])
#Check if the absolute difference matches the target
if absolute_difference == target_difference:
pair_count += 1
return pair_countInstead of checking every possible pair of numbers, the efficient solution keeps track of how many times each number appears. Then, it quickly calculates the number of pairs that meet the difference requirement using this information.
Here's how the algorithm would work step-by-step:
def count_number_of_pairs(number_list, difference):
number_counts = {}
pair_count = 0
for number in number_list:
if number in number_counts:
number_counts[number] += 1
else:
number_counts[number] = 1
# Iterate through each unique number
for number in number_counts:
# Check for the number 'k' greater
target_number_higher = number + difference
if target_number_higher in number_counts:
pair_count += number_counts[number] * number_counts[target_number_higher]
# Check for the number 'k' smaller
# Avoid double counting if k == 0
if difference != 0:
target_number_lower = number - difference
if target_number_lower in number_counts:
# Count pairs with the 'k' smaller number.
pair_count += number_counts[number] * number_counts[target_number_lower]
# Divide by 2 since each pair was counted twice.
return pair_count // 2| Case | How to Handle |
|---|---|
| Null input array | Throw an IllegalArgumentException or return 0 to indicate invalid input. |
| Empty input array | Return 0 since no pairs can be formed. |
| Array with one element | Return 0 as a pair requires at least two elements. |
| Large input array (performance) | Use a hashmap-based solution to achieve O(n) time complexity, avoiding nested loops. |
| k is zero | The solution should correctly count pairs with identical elements. |
| k is negative | Take the absolute value of k before processing, or handle negative differences appropriately in the comparison. |
| Integer overflow if numbers are very large | Ensure the data type used to store the difference does not overflow; consider using long if necessary. |
| Array contains duplicate numbers | The algorithm should accurately count pairs even with duplicate numbers in the input array. |