Skip to content

Commit

Permalink
Merge pull request akkupy#488 from Pradeep1409/Two_sum
Browse files Browse the repository at this point in the history
Two Sum code added
  • Loading branch information
akkupy authored Oct 1, 2023
2 parents eb7b808 + 596bde5 commit 303bbdf
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cpp/Two_Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <vector>
#include <unordered_map>
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 = {5,4,5,8,6,8,9,2,4,6,8,9,2,4,};
int target = 18;

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;
}

0 comments on commit 303bbdf

Please sign in to comment.