-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from walkoverr/main
create pairs with difference k
- Loading branch information
Showing
1 changed file
with
41 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,41 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int countPairsWithDiffK(vector<int>& arr, int k) { | ||
unordered_map<int, int> elementCount; | ||
int count = 0; | ||
|
||
// Populate the map with frequencies of each element. | ||
for (int num : arr) { | ||
elementCount[num]++; | ||
} | ||
|
||
// Iterate through each unique element in the map. | ||
for (const auto& element : elementCount) { | ||
int value = element.first; | ||
|
||
// If `value + k` exists in the map, add to count. | ||
if (elementCount.find(value + k) != elementCount.end()) { | ||
count += element.second * elementCount[value + k]; | ||
} | ||
} | ||
return count; | ||
} | ||
}; | ||
|
||
int main() { | ||
Solution solution; | ||
vector<int> arr1 = {1, 5, 3, 4, 2}; | ||
int k1 = 3; | ||
cout << "Number of pairs with difference " << k1 << ": " | ||
<< solution.countPairsWithDiffK(arr1, k1) << endl; | ||
|
||
vector<int> arr2 = {8, 12, 16, 4, 0, 20}; | ||
int k2 = 4; | ||
cout << "Number of pairs with difference " << k2 << ": " | ||
<< solution.countPairsWithDiffK(arr2, k2) <<endl; | ||
|
||
return 0; | ||
} |