Count Number of Pairs With Absolute Difference K
Problem Description​
You are 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:
xifx >= 0-xifx < 0
Examples​
Example 1:
Input: nums = [1,2,2,1], k = 1
Output: 4
Explanation: The pairs with an absolute difference of 1 are:
- (0,1)
- (0,2)
- (1,3)
- (2,3)
Example 2:
Input: nums = [1,3], k = 2
Output: 0
Explanation: There are no pairs with an absolute difference of 2.
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 <= 99
Approach​
To solve this problem efficiently:
- Use a hash map (
freq) to count occurrences of each number innums. - Iterate through each number
numinnums:- Check if
num - kexists infreq:- If yes, add
freq[num - k]to the count of valid pairs.
- If yes, add
- Check if
num + kexists infreq:- If yes (and
kis not zero to avoid double counting), addfreq[num + k]to the count.
- If yes (and
- Check if
- Update
freq[num]after processing each number to ensure correct counts for subsequent pairs.
This approach ensures that we count pairs in linear time O(n), where n is the length of nums, due to the efficient lookups and updates in the hash map.
C++​
class Solution {
public:
int countPairs(vector<int>& nums, int k) {
unordered_map<int, int> freq;
int count = 0;
for (int num : nums) {
if (freq.count(num - k)) {
count += freq[num - k];
}
if (freq.count(num + k) && k != 0) { // Avoid double counting when k == 0
count += freq[num + k];
}
freq[num]++;
}
return count;
}
};
Python​
def countPairs(nums, k):
freq = {}
count = 0
for num in nums:
if num - k in freq:
count += freq[num - k]
if num + k in freq and k != 0: # Avoid double counting when k == 0
count += freq[num + k]
if num in freq:
freq[num] += 1
else:
freq[num] = 1
return count