-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
solved GFG_POTD_30-10-24 Pairs with difference k
- Loading branch information
1 parent
34aafea
commit 0d3a0a7
Showing
1 changed file
with
19 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,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; | ||
} | ||
}; |