# MERGE_TWO - Editorial

Practice

Author: Kunal Demla
Editorialist: Shivansh Kapoor

Easy

Arrays

# PROBLEM:

Merge and sort into one; 2 sorted arrays.

# QUICK EXPLANATION:

We will take a third array and add the smallest element one by one from both the arrays.

# EXPLANATION:

We will take input of two arrays from the user and we will make a third array in which we will be storing the smallest element of the two arrays at the particular index. The array whose elements has been put into the third array, its index will be increased and when one array is complete. The other array will be simply added to the end at third array. We will simply print out the third array and we will get the desired output

# SOLUTIONS:

Setter's Solution
``````#include <bits/stdc++.h>
using namespace std;
#define ll long long int
void solve()
{
int i,j,n,k;
cin>>n>>k;
ll a[n];
ll b[k];
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<k;i++)
cin>>b[i];

vector<int> c;
i=j=0;
while(i<n&&j<k)
{
if(a[i]<b[j])
c.push_back(a[i++]);
else
c.push_back(b[j++]);
}
for(;j<k;j++)
c.push_back(b[j]);
for(;i<n;i++)
c.push_back(a[i]);
for(i=0;i<n+k;i++)
cout<<c[i]<<" ";
cout<<endl;

}

int main() {
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}
``````