-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathn_k_occurences.cpp
95 lines (79 loc) · 1.76 KB
/
n_k_occurences.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
Given an arr, and number k
print all the elements having more than n/k occurences
arr = [30, 10, 20, 20, 10, 20, 30, 30] k=4
op: 20 30
exp: n=8, k=2
n/k = 2
count of 20 = 3 > n/k
count of 30 = 3 > n/k
arr = [30, 10, 20, 30, 30, 40, 30, 40, 30] k=2
op: 30
exp: n=9, k=2
n/k = 4.5
all nos that appear 5 or more time
30 appears 5 times
*/
/*
sol1:
sort that array!
keep counting consecutive same elements
and check if count > n/k
*/
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
void printNByK(int arr[], int n, int k)
{
sort(arr, arr + n);
int i = 1, count = 1;
while(i < n)
{
while(i < n && arr[i] == arr[i-1])
{
++count;
++i;
}
if(count > n/k)
cout << arr[i - 1] << " ";
count = 1;
i++;
}
}
/*
sol2: O(n) time and space
use map lol
keys == arr els
values == freq
but if k <<<< n,
storing everything in a map might not be a good idea
*/
void printNByK_hashing(int arr[], int n, int k)
{
unordered_map<int, int> m;
for (int i = 0; i < n; i++)
m[arr[i]]++;
for(auto e: m)
if(e.second > n/k)
cout << e.first << " ";
}
/*
sol3: O(nk) time
through observation we know that no of elements in op are at most k-1
find candidate elements: els which can be possible op
then it validates whether candidates are actually final sol
This idea is based on Murray's boating algo?
- Create empty map
-
for(i = 0; i < n; i++)
- if m contains arr[i]
m[arr[i]]++
- else if m.size() < k-1
m.put(arr[i], 1)
- else
decrease all values in m by one
IF value becomes 0, remove it
- FOR all elements in m
print the elements that actually appear more than n/k times
*/