In this HackerEarth Chandu and his Girlfriend Returns problem solution In the previous problem Chandu bought some unsorted arrays and sorted them (in non-increasing order). Now, he has many sorted arrays to give to his girlfriend. But, the number of sorted arrays are very large so Chandu decided to merge two sorted arrays into one sorted array. But he is too lazy to do that. So, he asked your help to merge the two sorted arrays into one sorted array (in non-increasing order).
HackerEarth Chandu and his Girlfriend Returns problem solution.
#include <iostream>
#include <cassert>
#include <vector>
#include <cstdio>
using namespace std;
const int MAX = 1e5 + 5;
long long a[MAX], b[MAX];
vector <long long> c;
void merge1(int N, int M)
{
int x = 0, y = 0;
int k = N + M;
c.clear();
while(x < N and y < M)
{
if(a[x] > b[y])
c.push_back(a[x++]);
else
c.push_back(b[y++]);
}
while(x < N)
c.push_back(a[x++]);
while(y < M)
c.push_back(b[y++]);
}
int main()
{
int t, n, m;
scanf("%d", &t);
assert(1 <= t and t <= 100);
while(t--)
{
scanf("%d %d", &n, &m);
assert(1 <= n and n <= 50000);
assert(1 <= m and m <= 50000);
for(int i = 0;i < n;++i)
{
scanf("%lld", &a[i]);
assert(0 <= a[i] and a[i] <= 1000000000LL);
}
for(int i = 0;i < m;++i)
{
scanf("%lld", &b[i]);
assert(0 <= b[i] and b[i] <= 1000000000LL);
}
merge1(n, m);
for(int i = 0;i < c.size();++i)
if(i < c.size() - 1)
printf("%lld ", c[i]);
else
printf("%lldn", c[i]);
}
return 0;
}
Second solution
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define CLEAR(array, value) memset(ptr, value, sizeof(array));
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pi(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define pn printf("n")
#define SZ(x) (int)((x).size())
int main()
{
ios::sync_with_stdio(false);
int t;
//scanf("%d",&t);
cin>>t;
assert(1<=t && t<=100);
while(t--)
{
int i,j,k,n,m;
//scanf("%d",&n);
cin>>n>>m;
assert(1<=n<=50000);
assert(1<=m<=50000);
vector<int>va(n+m);
rep(i,n+m){cin>>va[i];
assert(0<=va[i]<=1000000000);
}
sort(va.rbegin(),va.rend());
rep(i,n+m)cout<<va[i]<<" ";
cout<<"n";
}
return 0;
}