-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnion of Two Sorted Arrays.cpp
More file actions
85 lines (85 loc) · 1.66 KB
/
Union of Two Sorted Arrays.cpp
File metadata and controls
85 lines (85 loc) · 1.66 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Union of two Arrays.
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
class Solution{
public:
vector<int> Find(vector<int>&V1,vector<int>&V2);
};
vector<int> Solution::Find(vector<int>&V1,vector<int>&V2){
bool var1=is_sorted(V1.begin(),V1.end());
bool var2=is_sorted(V2.begin(),V2.end());
if(!var1){
sort(V1.begin(),V1.end());
}
if(!var2){
sort(V2.begin(),V2.end());
}
vector<int>U;
int i,j;
i=j=0;
while((i<V1.size()) && (j<V2.size())){
if(V1[i]<V2[j]){
if(U.empty() || V1[i]!=U.back()){
U.push_back(V1[i]);
}
++i;
}
else if(V2[j]<V1[i]){
if(U.empty() || V2[j]!=U.back()){
U.push_back(V2[j]);
}
++j;
}
else{
if(U.empty() || V1[i]!=U.back()){
U.push_back(V1[i]);
}
while (i < V1.size() && V1[i] == V1[i+1]) i++;
while (j < V2.size() && V2[j] == V2[j+1]) j++;
i++;
j++;
}
}
while(i<V1.size()){
if(U.empty() || V1[i]!=U.back()){
U.push_back(V1[i]);
}
++i;
}
while(j<V2.size()){
if(U.empty() || V2[j]!=U.back()){
U.push_back(V2[j]);
}
++j;
}
return U;
}
int main(){
int n1,n2;
cout<<"Enter Size of First Array: ";
cin>>n1;
cout<<"Enter Size of Second Array: ";
cin>>n2;
vector<int>V1(n1);
vector<int>V2(n2);
cout<<endl;
for(int i=0;i<V1.size();i++){
cout<<"Enter "<<i+1<<" value of Array-1: ";
cin>>V1[i];
}
cout<<endl;
for(int i=0;i<V2.size(); i++){
cout<<"Enter "<<i+1<<" value of Array-2: ";
cin>>V2[i];
}
vector<int>U;
Solution obj;
U=obj.Find(V1,V2);
cout<<"Resultant Array\n";
for(int i=0;i<U.size();i++){
cout<<U[i]<<"\t";
}
return 0;
}