-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment1.cpp
More file actions
32 lines (25 loc) · 796 Bytes
/
Assignment1.cpp
File metadata and controls
32 lines (25 loc) · 796 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
#include <iostream>
#include <vector>
#include <unordered_map>
std::vector<int> twoSum(std::vector<int>& nums, int target) {
std::unordered_map<int, int> numIndexMap;
for (int i = 0; i < nums.size(); i++) {
int complement = target - nums[i];
if (numIndexMap.find(complement) != numIndexMap.end()) {
return {numIndexMap[complement], i};
}
numIndexMap[nums[i]] = i;
}
return {};
}
int main() {
std::vector<int> nums = {2, 7, 11, 15};
int target = 9;
std::vector<int> result = twoSum(nums, target);
if (result.size() == 2) {
std::cout << "Output: [" << result[0] << ", " << result[1] << "]" << std::endl;
} else {
std::cout << "No valid solution found." << std::endl;
}
return 0;
}