-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntersectionElements.cpp
80 lines (64 loc) · 1.39 KB
/
IntersectionElements.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<bits/stdc++.h>
using namespace std;
// Brute approach - TC - O(n*m) , SC - O(min(n,m));
vector<int> intersectionArrayElements(vector<int>& arr1, vector<int>& arr2)
{
int n = arr1.size();
int m = arr2.size();
vector<int> ans;
vector<int> visited(m+n , 0);
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
if(arr1[i] == arr2[j] && visited[j] == 0)
{
ans.push_back(arr2[j]);
visited[j] = -1;
break;
}
if(arr2[j] > arr1[i])
{
break;
}
}
}
return ans;
}
// Optimal approach - TC- O(min(n,m)) , SC - O(n)
vector<int> intersectionArrayElements(vector<int>& arr1, vector<int>& arr2)
{
vector<int> ans;
int n = arr1.size();
int m = arr2.size();
int i=0, j=0;
while(i<n && j<m)
{
if(arr1[i] == arr2[j])
{
ans.push_back(arr1[i]);
i++;
j++;
}
else if(arr1[i] < arr2[j])
{
i++;
}
else
{
j++;
}
}
return ans;
}
int main()
{
vector<int> arr1 = {1,2,2,3,3,4,5,6};
vector<int> arr2 = {2,3,3,5,6,6,7};
vector<int> Elements = intersectionArrayElements(arr1 , arr2);
for(int val : Elements)
{
cout<<val<<" ";
}
cout<<endl;
}