-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_1.cpp
More file actions
35 lines (32 loc) · 1.28 KB
/
leetcode_1.cpp
File metadata and controls
35 lines (32 loc) · 1.28 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
// LeetCode Problem 1: Two Sum
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// You can return the answer in any order.
// Example: Input: nums = [2,7,11,15], target = 9; Output: [0,1] (because nums[0] + nums[1] = 9)
class Solution
{
public:
// Function that takes a vector of integers and a target sum, returns indices of two numbers that add up to target
vector<int> twoSum(vector<int> &nums, int target)
{
// Create an empty vector to store the result indices
vector<int> myarr;
// Outer loop - pick the first number
for (int i = 0; i < nums.size(); i++)
{
// Inner loop - pick the second number (starts from i+1 to avoid using same element twice)
for (int j = i + 1; j < nums.size(); j++)
{
// If the sum of two numbers equals target
if (nums[i] + nums[j] == target)
{
myarr.push_back(i); // Add first index to result
myarr.push_back(j); // Add second index to result
return myarr; // Return the result immediately when found
}
}
}
// If no solution is found, return empty vector
return {};
}
};