1. Two Sums
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].Input: nums = [3,2,4], target = 6
Output: [1,2]Input: nums = [3,3], target = 6
Output: [0,1]Brute force solutions
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> v;
int s=0;
for(int i=0;i<nums.size();i++){
for(int j=i+1;j<nums.size();j++){
s=nums[i]+nums[j];
if(s==target)
{
v.push_back(i);
v.push_back(j);
break;
}
}
}
return v;
}
};Optimized solution using Hash Map
Two Sum II - Input array is sorted
Last updated