2006. Count Number of Pairs With Absolute Difference K

problem

solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int countKDifference(vector<int>& nums, int k) {
sort(nums.begin(), nums.end());
unordered_map<int,int> mp;
int count = 0;
for(int n:nums) mp[n]++;
int i=0, n=nums.size();
while(i<n){
// cout<<nums[i]<<" ";
if(mp.count(nums[i]+k)) count+= mp[nums[i]]*mp[nums[i]+k];

while(i+1<n && nums[i]==nums[i+1]) i++;
i++;

}
return count;
}
};

analysis

  • time complexity O(nlogn)
  • space complexity O(n)