Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added two sum solution in CPP using map #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions leetcode/1 - twosum/two_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <bits/stdc++.h>

using namespace std;

/**
* The Two Sum problem can be solved using a map to store each index
*
* In the following approach, the C++ Map can store each index in O (Log N)
* The trick in this question is start to store index from last index to index 1,
* just to deal with repeated values. The other point is just get all indexes in O (N)
*
* After that, you can loop through array getting the remainder and check if
* there is this velor in the array
*
* Complexity Time: O (N log N)
*
* Note: Its possible solve this problem using Binary Search as well,
* you just need to be careful with negative values. The complexity time
* will be O (N log N) as well.
*
*/

vector<int> twoSum(vector<int>& nums, int target) {
map<int, int> mapAux;

for(int i = nums.size() - 1; i >= 1; i--) {
mapAux[nums[i]] = i;
}

for(int i = 0; i < nums.size(); i++) {
int rem = target - nums[i];
int remPos = mapAux[rem];

if(nums[remPos] == rem && remPos != i) {
vector<int> answer = {i, remPos};

return answer;
}
}

return vector<int>();
}

int main () {

vector<int> nums = {2, 7, 11, 15};
int target = 9;

auto answer = twoSum(nums, target);

cout << answer[0] << " " << answer[1] << endl;

return 0;
}