Skip to content

Commit

Permalink
solved GFG_POTD_30-10-24 Pairs with difference k
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamjag7 authored Oct 30, 2024
1 parent 34aafea commit 0d3a0a7
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions october_2024/GFG_POTD_30-10-24.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// User function template for C++
class Solution {
public:
/* Returns count of pairs with difference k */

int countPairsWithDiffK(vector<int>& arr, int k) {
int cnt=0;
map<int,int>mpp;
for(int i=0;i<arr.size();i++){
mpp[arr[i]]++;
}
for(int i=0;i<arr.size();i++){
if(arr[i]-k>0 && mpp.find(arr[i]-k)!=mpp.end()){
cnt+=mpp[arr[i]-k];
}
}
return cnt;
}
};

0 comments on commit 0d3a0a7

Please sign in to comment.