forked from peehu-gandhi/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunion_of_2_sorted_arrays.cpp
57 lines (53 loc) · 1.11 KB
/
union_of_2_sorted_arrays.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
#include<iostream>
#include<cmath>
#include<bits/stdc++.h>
using namespace std;
void unionOfArrays(int arr1[], int arr2[], int n1, int n2){
int i=0,j=0;
while(i < n1 && j < n2){
if(arr1[i]<arr2[j]){
cout<<arr1[i]<<" ";
i++;
}
else if(arr1[i]>arr2[j]){
cout<<arr2[j]<<" ";
j++;
}
else{
cout<<arr1[i]<<" ";
i++;
j++;
}
}
while(i < n1){
cout<<arr1[i]<<" ";
i++;
}
while(j < n2){
cout<<arr2[j]<<" ";
j++;
}
}
int main(){
int n1,n2;
cout<<"Enter size of array-1 : ";
cin>>n1;
int arr1[n1];
cout<<"Enter array-1 elements : ";
for(int i=0;i<n1;i++){
cin>>arr1[i];
}
cout<<"Enter size of array-2 : ";
cin>>n2;
int arr2[n2];
cout<<"Enter array-2 elements : ";
for(int i=0;i<n2;i++){
cin>>arr2[i];
}
/*
arr1[] = {1 3 4 5 6 7}
arr2[] = {2 4 8 9}
*/
unionOfArrays(arr1,arr2,n1,n2);
return 0;
}