-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindPairWithGivenDifference.cpp
More file actions
46 lines (40 loc) · 1021 Bytes
/
FindPairWithGivenDifference.cpp
File metadata and controls
46 lines (40 loc) · 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <vector>
#include <unordered_map>
#include <set>
class PairFinder
{
public:
std::set<std::pair<int, int>> find(const std::vector<int>& arr, int diff)
{
// map of array element and its index
std::unordered_map<int, int> map;
std::set<std::pair<int, int>> result;
for (size_t i = 0; i < arr.size(); ++i)
{
auto found1 = map.find(arr[i] - diff);
auto found2 = map.find(arr[i] + diff);
if (found1 != map.end()) {
result.insert(std::make_pair(found1->first, arr[i]));
} else if (found2 != map.end()) {
result.insert(std::make_pair(found2->first, arr[i]));
} else {
map[arr[i]] = i;
}
}
return result;
}
};
int main()
{
std::vector<int> arr{1, 5, 2, 2, 2, 5, 5, 4};
int k = 3;
PairFinder obj;
std::set<std::pair<int, int>> result = obj.find(arr, k);
std::cout << "Pairs with difference " << k << " are:" << std::endl;
for (auto iter : result)
{
std::cout << "(" << iter.first << ", " << iter.second << ")" << std::endl;
}
return 0;
}