-
Notifications
You must be signed in to change notification settings - Fork 2
/
find the union.cpp
64 lines (63 loc) · 1.25 KB
/
find the union.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
58
59
60
61
62
63
64
#include<bits/stdc++.h>
vector < int > sortedArray(vector < int > a, vector < int > b) {
int n=a.size();
int m=b.size();
vector<int>v;
int i=0,j=0;
if(a[i]<b[j])
{
v.push_back(a[i]);
i++;
}
else if(b[j]<a[i])
{
v.push_back(b[j]);
j++;
}
else if(b[j]==a[i])
{
v.push_back(a[i]);
i++;
j++;
}
while(i<n && j<m)
{
if(a[i]<b[j] )
{ if(a[i]== v[v.size()-1])
i++;
else{
v.push_back(a[i]);
i++;
}
}
else if(b[j]<a[i])
{ if(b[j]== v[v.size()-1])
j++;
else {v.push_back(b[j]);
j++;
}
}
else if(b[j]==a[i])
{if(a[i]== v[v.size()-1])
i++;
else{v.push_back(a[i]);
i++;
j++;
}
}
}
for(;i<n;i++)
{ if(a[i]== v[v.size()-1])
i++;
else{ v.push_back(a[i]);
}
}
for(;j<m;j++)
{ if(b[j]== v[v.size()-1])
j++;
else{
v.push_back(b[j]);
}
}
return v;
}