-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintersectionarray.cpp
More file actions
51 lines (47 loc) · 1.32 KB
/
intersectionarray.cpp
File metadata and controls
51 lines (47 loc) · 1.32 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
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> A = {1,2,2,3,3,4,5,6};
int n = A.size();
vector<int> B = {2,3,3,5,6,6,7};
int m = B.size();
vector<int>ans;
//====================THE BRUTE FORCE APPROACH=================
// int vis[m] = {0};
// for(int i=0;i<n;i++){ // take ith element of array A
// for(int j=0;j<m;j++){ // ith element of array A is compared in whole array B
// if(A[i] == B[j] && vis[j]==0){ //here we check ith element of A is equal to the jth element of B and that element of B is not repeated before
// ans.push_back(A[i]);
// vis[j]=1;
// break;
// }
// if(B[j] > A[i]){
// break;
// }
// }
// }
// for(auto i:ans){
// cout<<i<<" ";
// }
//============================THE OPTIMAL APPROACH==================
int i=0;
int j=0;
while(i<n && j<m){
if(A[i]<B[j]){
i++;
}
else if(B[j] < A[i]){
j++;
}
else{
ans.push_back(A[i]);
i++;
j++;
}
}
for(auto i:ans){
cout<<i<<" ";
}
return 0;
}