-
Notifications
You must be signed in to change notification settings - Fork 1
/
commen_ele.cpp
58 lines (56 loc) · 1.43 KB
/
commen_ele.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
#include<iostream>
#include<vector>
using namespace std;
vector<int> v;
vector <int> commonElements (int a[], int b[], int n1, int n2)
{
//code here.
int i=0,j=0;
while(i<n1 && j<n2){
if(a[i]<b[i]){
i++;
}
else if(a[i]>b[i]){
j++;
}
else{
v.push_back(a[i]);
i++;
}
}
return v;
// finder(A,B,n1,n2,v);
// int length =v.size();
// int arr[length];
// for(int i=0;i<length;i++){
// arr[i]=v[length-1-i];
// v.pop_back();
// }
// finder(arr,C,length,n3,v);
return v;
}
void finder(int a[],int b[],int n1,int n2,vector<int>&v){
int i=0,j=0;
while(i<n1 && j<n2){
if(a[i]<b[i]){
i++;
}
else if(a[i]>b[i]){
j++;
}
else{
v.push_back(a[i]);
i++;
}
}
}
int main(){
// vector<int> v;
int a[]={2,3,20,25,80};
int b[]={1,4,15,20,80,300};
commonElements(a,b,5,6);
for(auto ele:v){
cout<<ele<<" ";
}
return 0;
}