-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathTwo_sum.cpp
More file actions
37 lines (30 loc) · 1.09 KB
/
Two_sum.cpp
File metadata and controls
37 lines (30 loc) · 1.09 KB
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
#include <bits/stdc++.h>
using namespace std;
// Function to find two numbers that add up to a target sum
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> Nums; // Store number-index pairs
vector<int> result; // Store the result indices
// Iterate through the input vector
for (int i = 0; i < nums.size(); ++i) {
int Diff = target - nums[i]; // Calculate the Diff
if (Nums.find( Diff) != Nums.end()) {
// If Diff exists in the map, we found a solution
result.push_back(Nums[ Diff]);
result.push_back(i);
break; // Exit the loop
}
Nums[nums[i]] = i; // Store the current number and its index
}
return result;
}
int main() {
vector<int> nums = {72,4,2,5,3,4,2,15,3,16};
int target = 9;
vector<int> result = twoSum(nums, target);
if (result.size() == 2) {
cout << "Indices of the two numbers that add up to " << target << ": " << result[0] << " and " << result[1] << endl;
} else {
cout << "No solution found." << endl;
}
return 0;
}