-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtwo sum.c++
40 lines (31 loc) · 969 Bytes
/
two sum.c++
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
#include <bits/stdc++.h>
using namespace std;
// Function to check whether any pair exists
// whose sum is equal to the given target value
bool twoSum(vector<int> &arr, int target){
// Create an unordered_set to store the elements
unordered_set<int> s;
// Iterate through each element in the vector
for (int i = 0; i < arr.size(); i++){
// Calculate the complement that added to
// arr[i], equals the target
int complement = target - arr[i];
// Check if the complement exists in the set
if (s.find(complement) != s.end())
return true;
// Add the current element to the set
s.insert(arr[i]);
}
// If no pair is found
return false;
}
int main(){
vector<int> arr = {0, -1, 2, -3, 1};
int target = -2;
// Call the twoSum function and print the result
if (twoSum(arr, target))
cout << "true";
else
cout << "false";
return 0;
}