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
#include <iostream>
#include <unordered_map>
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char const *argv[])
{
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int n;
    std::cin >> n;

    std::unordered_map<int, int> frequency;

    for (int i = 0; i < n; i++) {
        int num;
        std::cin >> num;
        if (frequency.find(num) == frequency.end()) 
        {
            frequency.insert(std::make_pair(num, 1));
        }
        // Else update the frequency
        else
        {
            frequency[num]++;
        }
    }

    // Sort the frequency map by value
    std::vector<std::pair<int, int>> frequency_vector(frequency.begin(), frequency.end());

    // Print the sorted frequency map
    // for (const auto& pair : frequency_vector) {
    //     std::cout << pair.first << ": " << pair.second << std::endl;
    // }

    int start = 0;
    int end = frequency_vector.size() - 1;
    int result = 0;

    while (start <= end) {
        //cout << start << " - " << end << endl;
        int leader_count = frequency_vector[end].second - 1;
        //cout << "leader_count = " << leader_count << endl;

        while (frequency_vector[start].second <= leader_count && start <= end) {
            leader_count -= frequency_vector[start].second;
            start++;
            //cout << "reducing leader_count " << leader_count << endl;
        }
        if (start <= end && leader_count > 0 && frequency_vector[start].second > leader_count) {
            frequency_vector[start].second -= leader_count;
            //cout << "reducing fv at position " << start << " new value " << frequency_vector[start].second << endl;
        }
        
        end--;
        result++;
    }

    std::cout << result << std::endl;

    return 0;
}