-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.cpp
83 lines (74 loc) · 2.72 KB
/
code.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* **************************************************************************
* Copyright 2024 The OpenGenus.org Authors. All Rights Reserved.
*
* Code for the book "DAILY 43: Algorithmic Problems for Coding Interviews: Easy level, C++ edition"
*
* Licensed under the GNU General Public License, Version 3.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.gnu.org/licenses/gpl-3.0.html
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For details about the book, please visit: https://www.amazon.com/dp/B0CZJNBLQS
* *************************************************************************/
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
// Index of two sum
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> HashSet;
int n = nums.size();
for (int i = 0; i < n; i++) {
int complement = target - nums[i];
// Find if the target sum is achieved
if (HashSet.count(complement)) {
return {HashSet[complement], i};
}
// Insert current element in set with index
HashSet[nums[i]] = i;
}
return {};
}
int main() {
// Input data: 2D array with each row representing a test case
vector<vector<int>> inputs = {
{2, 7, 11, 15, 3}, // Test Case 1
{3, 2, 4}, // Test Case 2
{3, 3}, // Test Case 3
{-1, -2, -3, -4, -5}, // Test Case 4
{0, 4, 3, 0}, // Test Case 5
};
// Target values corresponding to each test case
vector<int> targets = {9, 6, 6, -8, 0};
// Process each test case
for (int i = 0; i < inputs.size(); ++i) {
try {
// Call the twoSum function
vector<int> result = twoSum(inputs[i], targets[i]);
// Print the input and output
cout << "Test Case " << i + 1 << ":\n";
cout << "Input: [";
for (int num : inputs[i]) {
cout << num << " ";
}
cout << "]\n";
cout << "Target: " << targets[i] << "\n";
// Output the result
cout << "Output: [";
for (int num : result) {
cout << num << " ";
}
cout << "]\n\n";
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
return 0;
}