-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathequalizeArray.cpp
More file actions
50 lines (36 loc) · 892 Bytes
/
equalizeArray.cpp
File metadata and controls
50 lines (36 loc) · 892 Bytes
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
/*
Problem statement:
Given an array of integers, determine the minimum number of elements to delete to leave only elements of equal value.
Example:
arr = [1, 2, 3, 4]
Delete the 2 elements 1 and 3 leaving arr = [2, 2].
If both twos plus either the 1 or the 3 are deleted, it takes 3 deletions to leave either [3] or [1].
The minimum number of deletions is 2.
*/
#include <bits/stdc++.h>
using namespace std;
int findMaxOccurence (map<int, int> &list) {
int max = 0;
for (map<int, int>::iterator i = list.begin(); i != list.end(); ++i) {
if ((*i).second > max)
max = (*i).second;
}
return max;
}
int main () {
int n, elem;
cin >> n;
int p = n;
map<int, int> list;
while (n--) {
cin >> elem;
list[elem]++;
}
cout << p - findMaxOccurence(list);
return 0;
}
/*
Sample test case:
5
3 3 2 1 3
*/