-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge2aarrays.cpp
More file actions
51 lines (37 loc) · 1 KB
/
merge2aarrays.cpp
File metadata and controls
51 lines (37 loc) · 1 KB
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
#include <bits/stdc++.h>
using namespace std;
void merge(long long ar1[], long long ar2[], long long m, int n)
{
for (long long i=n-1; i>=0; i--)
{
long long j, last = ar1[m-1];
//Merging Process
for (j=m-2; j >= 0 && ar1[j] > ar2[i]; j--)
ar1[j+1] = ar1[j];
if (j != m-2 || last > ar2[i])
{
ar1[j+1] = ar2[i];
ar2[i] = last;
}
}
}
int main(void)
{
long long test,m,n,ar1[100001],ar2[100001],i;
cin>>test;
while(test--){
cin>>m>>n;
for (i=0; i<m; i++)
cin>>ar1[i];
for (i=0; i<n; i++)
cin>>ar2[i];
merge(ar1, ar2, m, n); // Merge Operation
for (i=0; i<m; i++)
cout << ar1[i] << " "; //Printing Array 1
cout << "\n";
for (i=0; i<n; i++)
cout << ar2[i] << " "; //Printing Array 2
cout<<"\n";
}
return 0;
}