-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDAY 16 TWO SUM .CPP
58 lines (53 loc) · 1.49 KB
/
DAY 16 TWO SUM .CPP
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// TWO SUM.CPP LEETCODE SOLUTION
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
/* int left=0, right=int(nums.size())-1,tempsum;
while(left<right)
{
tempsum=nums[left]+nums[right];
if(tempsum==target)
return {left+1, right+1};
if(tempsum>target)
right--;
else
left++;
}
return {-1,-1};
}
int main()
{
vector<int>nums={1,4,5,11,12};
int target=9;
for(int &x:twoSum(nums,target))
cout<<x<<" ";
cout<<endl;
*/
map<int,int> m;
vector<int> v;
for(int i=0; i<nums.size(); i++)
{
if(m.find(target-nums[i] )!= m.end())
{
v.push_back(m[target-nums[i]]);
v.push_back(i);
return v;
}
m[nums[i]]=i;
}
return v;
}
};
// MERGE TWO SORTED ARRAY.CPP LEET CODE
class Solution {
public:
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int r=m+n-1;// both array merge and subtract-1 value after merge array
int r1=m-1;//first array
int r2=n-1;//second array
while(r1>=0 && r2>=0) // r1 value greater then or equal to 0 and r2 greater then or equal 0
nums1[r--]=nums1[r1] > nums2[r2]?nums1[r1--]:nums2[r2--];//
while(r2>=0)
nums1[r--]=nums2[r2--];
}
};