-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolutioncpp.cpp
More file actions
64 lines (52 loc) · 1.21 KB
/
Solutioncpp.cpp
File metadata and controls
64 lines (52 loc) · 1.21 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
//First method that is Naive Approach
//Time Complexity -> O(n^2)
//Space Complexity -> O(1)
#include<iostream>
using namespace std;
void findElement(int a[] , int b[], int n1, int n2){
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
if(a[i]==b[j]){
break;
}
if(j==n2-1){
cout<<a[i]<<" ";
}
}
}
}
int main()
{
int a[]={1, 2, 3, 4, 5, 10};
int b[]={2, 3, 1, 0, 5};
int n1 = sizeof(a) / sizeof(a[0]);
int n2 = sizeof(b) / sizeof(b[0]);
findElement(a, b, n1, n2);
return 0;
}
//Second method Using hashing
//Time Complexity -> O(n)
//Space Complexity -> O(n)
#include<bits/stdc++.h>
using namespace std;
void findElement(int a[], int b[],int n1, int n2)
{
//copy the second array in a hash table.
unordered_set <int> s;
for (int i = 0; i < n2; i++)
s.insert(b[i]);
//Print the element that are not present in hash table
for (int i = 0; i < n1; i++)
if (s.find(a[i]) == s.end()){
cout << a[i] << " ";
}
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 10};
int b[] = {2, 3, 1, 0, 5};
int n1 = sizeof(a) / sizeof(a[0]);
int n2 = sizeof(b) / sizeof(b[0]);
findElement(a, b, n1, n2);
return 0;
}