diff --git a/LeetCode/532.k-diff-pairs-in-an-array.cpp b/LeetCode/532.k-diff-pairs-in-an-array.cpp new file mode 100644 index 0000000..eda9324 --- /dev/null +++ b/LeetCode/532.k-diff-pairs-in-an-array.cpp @@ -0,0 +1,21 @@ +using hashmap = unordered_map; + +class Solution { +public: + int findPairs(vector& 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; + } +};