Maximize frequency of a component by including X to a Subsequence


#embrace <bits/stdc++.h>

utilizing namespace std;

  

void find_most_duplicates(vector<int>& A)

{

    int N = A.measurement();

    vector<pair<int, int> > freq;

  

    

    

    unordered_map<int, int> mp;

  

    

    for (int i = 0; i < A.measurement(); i++) {

        mp[A[i]]++;

    }

  

    

    

    for (auto i : mp) {

        freq.push_back({ i.second, i.first });

    }

    int max_freq = 0, X = 0, m = freq.measurement();

    if (m == 1) {

        

        max_freq = freq.again().first;

        X = 0;

    }

    else if (m == 2) {

        max_freq = freq[0].first + freq[1].first;

  

        

        

        

        X = abs(freq[0].second - freq[1].second);

    }

    else {

        int first_greatest = 0, second_greatest = 0;

        int first_greatest_index = -1,

            second_greatest_index = -1;

        for (int i = 0; i < m; i++) {

            if (freq[i].first > first_greatest) {

                swap(first_greatest, second_greatest);

                swap(first_greatest_index,

                     second_greatest_index);

                first_greatest = freq[i].first;

                first_greatest_index = i;

            }

            else if (freq[i].first > second_greatest) {

                second_greatest = freq[i].first;

                second_greatest_index = i;

            }

        }

        max_freq = first_greatest + second_greatest;

        X = abs(freq[first_greatest_index].second

                - freq[second_greatest_index].second);

    }

  

    

    cout << "X = " << X

         << ", Most Frequency Doable =  " << max_freq

         << endl;

}

  

int major()

{

    

    vector<int> A{ 2, 1, 3, 3, 4, 5 };

    find_most_duplicates(A);

    return 0;

}