Skip to content

Commit 360a9e1

Browse files
rachitiitrrachitjn
authored andcommitted
Day3 Coding Interview || k-diff-pairs-in-an-array
Easy
1 parent a4ac7bb commit 360a9e1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using hashmap = unordered_map<int, int>;
2+
3+
class Solution {
4+
public:
5+
int findPairs(vector<int>& nums, int k) {
6+
hashmap cnt;
7+
for(int x: nums) cnt[x]++;
8+
9+
int ans = 0;
10+
for(auto p: cnt) { // iterating on unique numbers of the array
11+
int x = p.first;
12+
// check x+k exists in the array
13+
if(cnt.find(x+k) == cnt.end()) {
14+
continue;
15+
}
16+
ans += (k==0) ? cnt[x+k] >= 2 : cnt[x+k] >= 1;
17+
}
18+
19+
return ans;
20+
}
21+
};

0 commit comments

Comments
 (0)