-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighLow.cpp
More file actions
96 lines (76 loc) · 2.22 KB
/
Copy pathHighLow.cpp
File metadata and controls
96 lines (76 loc) · 2.22 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
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
96
#include<unordered_map>
#include<vector>
#include<limits.h>
#include<iostream>
#include<limits>
using namespace std;
class Solution {
public:
pair<int, int> highestAndLowestFrequency(const vector<int>& arr) {
unordered_map<int, int> freq_map;
for (int num : arr) {
freq_map[num]++;
}
int max_freq = 0;
int min_freq = numeric_limits<int>::max();
int max_freq_ele = numeric_limits<int>::max();
int min_freq_element = numeric_limits<int>::max();
for (const auto& it : freq_map) {
int element = it.first;
int freq = it.second;
if (freq > max_freq || (freq == max_freq && element < max_freq_ele)) {
max_freq = freq;
max_freq_ele = element;
}
if (freq < min_freq || (freq == min_freq && element < min_freq_element)) {
min_freq = freq;
min_freq_element = element;
}
}
return {max_freq_ele, min_freq_element};
}
};
int main() {
Solution sol;
vector<int> arr = {3, 1, 2, 2, 4, 4, 4, 5, 5};
pair<int, int> result = sol.highestAndLowestFrequency(arr);
cout << "Max frequency element: " << result.first << endl;
cout << "Min frequency element: " << result.second << endl;
return 0;
}
// pair<int, int> highestAndLowestFrequency(const vector<int>& arr) {
// vector<int> v = arr;
//sort(v.begin(), v.end()); // ✅ MUST sort to group identical elements
// int n = v.size();
// int cf = 1;
// int maxFreq = 1;
// int minFreq = INT_MAX;
// int mode = v[0];
// int mode1 = v[0];
// for(int i = 1; i < n ; i++){
// if(v[i] == v[i - 1]){
// cf++;
// }
// else{
// if(cf > maxFreq){
// maxFreq = cf;
// mode = v[i - 1];
// }
// if(cf < minFreq){
// minFreq = cf;
// mode1 = v[i - 1];
// }
// cf = 1;
//}
// }
// if(cf > maxFreq){
// maxFreq = cf;
// mode = v[n-1];
// }
// if(cf < minFreq){
// minFreq = cf;
// mode1 = v[n-1];
// }
// return {mode, mode1};
//}
//};