-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathHasing.cpp
More file actions
69 lines (61 loc) · 1.49 KB
/
Hasing.cpp
File metadata and controls
69 lines (61 loc) · 1.49 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
65
66
67
68
69
/*
Given an array of size N integer , Give Q querues and in each query given a number x .
print count of that number in arraay.
consrains :
1 <= N <= 10^5
1 <= a[i] <= 10^7
1 <= Q <= 10^5
*/
#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e7+ 10;
int has_arr[N];
int main()
{
/*
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int q;
cin >> q;
while (q--)
{
int x, count = 0;
cin >> x;
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
{
count++;
}
}
cout << count << endl;
}
*/
// Time complexity.
// o(N) + o(Q*N) = o(N) cause Q and N are equal . so it will take time for input 10^10 .
// it won't work for 1 sec , only 10^7 can execute on online plateforms.
// To reduce this we will use precompution , Hasing a form of pre-compution.
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
has_arr[arr[i]]++; // For hasing . we are already counting value of count. at which index value is present
}
int q;
cin >> q;
while (q--)
{
int x;
cin >> x;
cout<<has_arr[x]<<endl;
}
// Time complexity = o(N) + o(Q) = 2* o(N) = 2 * 10^5
return 0;
}