-
Notifications
You must be signed in to change notification settings - Fork 991
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day3 Coding Interview || k-diff-pairs-in-an-array
Easy
- Loading branch information
1 parent
a4ac7bb
commit b724967
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |