Skip to content

Commit

Permalink
Merge pull request #6760 from TheNourhan/add-solution-to-two-sum-on-l…
Browse files Browse the repository at this point in the history
…eetCode

add solution to (two sum) problem on leetCode with c++
  • Loading branch information
AdiChat authored Nov 10, 2023
2 parents 70a7d4d + cd512f2 commit a57a06e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions code/online_challenges/src/leetcode/two_sum/two_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<bits/stdc++.h>
using namespace std;
//O(n^2)
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target){
vector<int>res;

for(int i=0;i<(int)nums.size();++i){
for(int j=i+1;j<(int)nums.size();++j){
if(nums[i]+nums[j]==target){
res.push_back(i);
res.push_back(j);
return res;
}
}
}

return res;
}
};

int main(){
vector<int>numbers {2,7,11,15};
vector<int>result;

result = Solution().twoSum(numbers, 9);

for(int i=0;i<(int)result.size();++i){
cout<<result[i]<< " ";
}

return 0;
}

0 comments on commit a57a06e

Please sign in to comment.