-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge Without Extra Space.cpp
85 lines (69 loc) · 1.8 KB
/
Merge Without Extra Space.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
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
int space(int n)
{
return(n<=1) ? 0 :(n/2) + (n%2);
}
void mergeArrays(vector<int>& a, vector<int>& b) {
// code here
int n = a.size(),m=b.size(),k=n+m;
for(k=space(k);k>0;k=space(k))
{
int i,j;
for(i=0;i+k<n;i++)
{
if(a[i]>a[i+k]) swap(a[i],a[i+k]);
}
for(j=(k>n ? k-n:0);i<n && j<m;i++,j++){
if(a[i]>b[j]) swap(a[i],b[j]);
}
for(j=0;j+k<m;j++)
{
if(b[j]>b[j+k]) swap(b[j],b[j+k]);
}
}
}
};
//{ Driver Code Starts.
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t; // Inputting the test cases
while (t--) {
vector<int> a, b;
// Reading the first array as a space-separated line
string arr1;
getline(cin >> ws, arr1); // Use ws to ignore any leading whitespace
stringstream ss1(arr1);
int num;
while (ss1 >> num) {
a.push_back(num);
}
// Reading the second array as a space-separated line
string arr2;
getline(cin, arr2);
stringstream ss2(arr2);
while (ss2 >> num) {
b.push_back(num);
}
Solution ob;
ob.mergeArrays(a, b);
// Output the merged result
for (int i = 0; i < a.size(); i++) {
cout << a[i] << " ";
}
cout << endl;
for (int i = 0; i < b.size(); i++) {
cout << b[i] << " ";
}
cout << "\n";
cout << "~\n";
}
return 0;
}
// } Driver Code Ends