-
Notifications
You must be signed in to change notification settings - Fork 2
/
mean_mode.cpp
51 lines (44 loc) · 1.37 KB
/
mean_mode.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
/*
Mean Mode
#hashmap #array
Have the function MeanMode(arr) take the array of numbers stored in arr and return
1 if the mode equals the mean, 0 if they don't equal each other (ie. [5, 3, 3, 3,
1] should return 1 because the mode (3) equals the mean (3)). The array will not
be empty, will only contain positive integers, and will not contain more than one
mode.
Optimal: o(n), achieved: o(n)
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
bool MeanMode(int arr[], int arrLength) {
// code goes here
std::unordered_map<int,size_t> mp;
int mean{};
auto it{mp.end()};
for (size_t i{}; i < arrLength; i++) {
// update hash map
it = mp.find(arr[i]);
if (it != mp.end()) {
it->second++;
} else {
mp[arr[i]] = 1;
}
mean += arr[i];
}
mean /= arrLength;
// sort map by value, by creating a vector and sorting it
std::vector<std::pair<int,size_t>> v{std::make_move_iterator(mp.begin()),
std::make_move_iterator(mp.end())};
std::sort(v.begin(),v.end(),[](auto p1, auto p2){return p1.second > p2.second;});
return mean == v.begin()->first;
}
int main(void) {
// keep this function call here
int A[] = coderbyteInternalStdinFunction(stdin);
int arrLength = sizeof(A) / sizeof(*A);
std::cout << MeanMode(A, arrLength);
return 0;
}