Skip to content

Commit

Permalink
Day3 Coding Interview || k-diff-pairs-in-an-array
Browse files Browse the repository at this point in the history
Easy
  • Loading branch information
rachitiitr authored and rachitjn committed Oct 4, 2020
1 parent a4ac7bb commit b724967
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LeetCode/532.k-diff-pairs-in-an-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using hashmap = unordered_map<int, int>;

class Solution {
public:
int findPairs(vector<int>& nums, int k) {
hashmap cnt;
for(int x: nums) cnt[x]++;

int ans = 0;
for(auto p: cnt) { // iterating on unique numbers of the array
int x = p.first;
// check x+k exists in the array
if(cnt.find(x+k) == cnt.end()) {
continue;
}
ans += (k==0) ? cnt[x+k] >= 2 : cnt[x+k] >= 1;
}

return ans;
}
};

0 comments on commit b724967

Please sign in to comment.