Skip to content

Commit e2450cc

Browse files
committed
Merging 2 sorted array
1 parent f329067 commit e2450cc

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Merging_Two_Sorted_Array/question.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Given two sorted arrays arr1[] and arr2[] in non-decreasing order with size n and m. The task is to merge the two sorted arrays into one sorted array (in non-decreasing order)

Merging_Two_Sorted_Array/solution.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main()
4+
{
5+
int t;
6+
cin>>t;
7+
while(t--)
8+
{
9+
int x,y;
10+
cin>>x>>y;
11+
long a[x+1],b[y+1];
12+
for(int i=0;i<x;i++)
13+
cin>>a[i];
14+
for(int i=0;i<y;i++)
15+
cin>>b[i];
16+
17+
int i=0,j=0;
18+
vector<long> vec;
19+
while(i<x&&j<y)
20+
{
21+
if(a[i]<b[j])
22+
{
23+
vec.push_back(a[i]);
24+
i++;
25+
}
26+
else if(a[i]>b[j])
27+
{
28+
vec.push_back(b[j]);
29+
j++;
30+
}
31+
else if(a[i]==b[j])
32+
{
33+
vec.push_back(a[i]);
34+
vec.push_back(b[j]);
35+
i++;
36+
j++;
37+
}
38+
}
39+
while(i<x)
40+
{
41+
vec.push_back(a[i]);
42+
i++;
43+
}
44+
while(j<y){
45+
vec.push_back(b[j]);
46+
j++;
47+
}
48+
for(int i=0;i<vec.size();i++)
49+
cout<<vec[i]<<" ";
50+
51+
cout<<endl;
52+
}
53+
return 0;
54+
}

0 commit comments

Comments
 (0)