-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpair_sum_k.cpp
32 lines (25 loc) · 1.15 KB
/
pair_sum_k.cpp
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
#include<bits/stdc++.h>
using namespace std;
vector<pair<int, int>> find_pairs_with_sum_k(const vector<int>& nums, const int k){
vector<pair<int, int>> result;
auto maximum = max_element(nums.begin(), nums.end()); // Need max element for sizing the hash table efficiently
vector<int> hash_table(*maximum+1, -1);
// Go through each element checking its counterpart's presence, also adding it to the hash table
for(int i = 0; i < nums.size(); i++){
if(hash_table[k - nums[i]] != -1) result.push_back(make_pair(hash_table[k - nums[i]], i));
hash_table[nums[i]] = i;
}
return result;
}
int main(){
// Example array and target sum
vector<int> nums = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int k = 22;
vector<pair<int, int>> pairs = find_pairs_with_sum_k(nums, k);
cout << "The array is: [";
for(int num: nums) cout << num << " ";
cout << "]" << endl << "Elements in the array that sum up to " << k << " are:" << endl;
for(pair<int, int> p: pairs){
cout << "Indices " << p.first << " and " << p.second << ", which are " << nums[p.first] << " and " << nums[p.second] << endl;
}
}