-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergeTowSortedArrWithOutExt.cpp
131 lines (127 loc) · 3.7 KB
/
mergeTowSortedArrWithOutExt.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<bits/stdc++.h>
using namespace std;
// class Solution{
// public:
// vector<int> mergeTwoSortedListWithOutExtraSpace(vector<int> arr1, vector<int> arr2)
// {
// int n = arr1.size();
// int m = arr2.size();
// vector<int> arr3(n+m);
// int left = 0;
// int right = 0;
// int idx = 0;
// while(left < n && right < m)
// {
// if(arr1[left] <= arr2[right])
// {
// arr3[idx] = arr1[left];
// idx++;
// left++;
// }
// else{
// arr3[idx]= arr2[right];
// idx++;
// right++;
// }
// }
// while(left < n)
// arr3[idx++] = arr1[left++];
// while(right < m)
// arr3[idx++] = arr2[right++];
// for(int i = 0; i< m+n; i++)
// {
// if(i < n)
// {
// arr1[i] = arr3[i];
// }
// else{
// arr2[i-n] = arr3[i];
// }
// }
// return arr1;
// }
// };
// this is the brute force solution whose time complexity is o(n+m)+o(n+m) and space complexity is o(n+m).
// class Solution{
// public:
// vector<int> mergeTwoSortedListWithOutExtraSpace(vector<int> arr1, vector<int> arr2)
// {
// int n = arr1.size();
// int m = arr2.size();
// int left = n -1;
// int right = 0;
// while(left >= 0 && right < m)
// {
// if(arr1[left] > arr2[right])
// {
// swap(arr1[left], arr2[right]);
// left--;
// right++;
// }
// else
// {
// break;
// }
// }
// sort(arr1.begin(), arr1.end());
// sort(arr2.begin(), arr2.end());
// return arr1;
// }
// };
// this is the better solution and the time complexity is o(min(n, m))+o(nlogn)+o(nlogn) and spce complexity is o(1).
class Solution{
private:
void swapIfGreater(vector<int>& arr1, vector<int>& arr2, int i, int j) {
if (arr1[i] > arr2[j]) {
swap(arr1[i], arr2[j]);
}
}
public:
vector<int> mergeTwoSortedListWithOutExtraSpace(vector<int>& arr1, vector<int>& arr2)
{
int n = arr1.size();
int m = arr2.size();
int len = n+m;
int gap = (len/2) + (len%2);
// this help us to give the selling value -> 9/2 = 4.5 = 5
while(gap > 0)
{
int left = 0;
int right = left + gap;
while(right < len)
{
if(left < n && right >= n)
{
swapIfGreater(arr1, arr2, left, right - n);
}
else if(left >= n)
{
swapIfGreater(arr2, arr2, left - n, right - n);
}
else{
swapIfGreater(arr1, arr1, left, right);
}
left++;
right ++;
}
if(gap == 1)
break;
gap = (gap/2) + (gap%2);
}
return arr1;
}
};
// this is the better solution whose time complexity is o(log2^(m+n))+o(n+m). and space is o(1).
int main()
{
vector<int> v1 = {1,3,5,7};
vector<int> v2 = {0, 2, 6, 8, 9};
Solution s;
vector<int> a = s.mergeTwoSortedListWithOutExtraSpace(v1, v2);
int n = a.size();
for(int i = 0; i< n; i++)
{
cout<< a[i]<< " ";
}
return 0;
}