-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_MergeWithoutExtraArray.cpp
111 lines (95 loc) · 2.43 KB
/
GFG_MergeWithoutExtraArray.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
/*
https://practice.geeksforgeeks.org/problems/merge-two-sorted-arrays5135/1#
Merge Without Extra Space
*/
// { Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
void merge(int arr1[], int arr2[], int n, int m) {
/*
int i=n-1;
int j=0;
while(i>=0 && j<m)
{
if(arr1[i] > arr2[j] )
swap(arr1[i--], arr2[j++]);
else
break; //no need to check further
// arr1_i will be smaller and arr2_j will be bigger
// j++;
// i--;
}
sort(arr1, arr1+n);
sort(arr2, arr2+m);
*/
/* USING Insertion Sort
//TLE at 249th TestCase
int i,j=0, first;
for(int i=0; i<n; i++)
{
//take one by one each ele of arr1
if(arr1[i]>arr2[0])
swap(arr1[i], arr2[0]);
//now sort the arr2 using insertion sort
first = arr2[0];
for( j=1; j<m && first>arr2[j]; j++)
arr2[j-1] = arr2[j];
arr2[j-1] = first;
}//end fun
*/
// /*
//USING GAP
int gap = ceil((float)(n+m)/2);
while(gap>0)
{
int i=0;
int j=gap;
while(j < (n+m))
{
if(j<n && arr1[i] > arr1[j])
swap(arr1[i], arr1[j]);
else if(j>=n && i<n && arr1[i] > arr2[j-n])
swap(arr1[i], arr2[j-n]);
else if(j>=n && i>=n && arr2[i-n] > arr2[j-n])
swap(arr2[i-n], arr2[j-n]);
i++;
j++;
}//while j < n+m
if(gap==1)
gap=0;
else
gap = ceil((float)gap/2);
}//end while gap
// */
}//end
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
int n, m, i;
cin >> n >> m;
int arr1[n], arr2[m];
for (i = 0; i < n; i++) {
cin >> arr1[i];
}
for (i = 0; i < m; i++) {
cin >> arr2[i];
}
Solution ob;
ob.merge(arr1, arr2, n, m);
for (i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
for (i = 0; i < m; i++) {
cout << arr2[i] << " ";
}
cout << "\n";
}
return 0;
} // } Driver Code Ends