Sunday, January 29, 2023
HomeSoftware DevelopmentMost attainable dimension of subset following the given constraints

Most attainable dimension of subset following the given constraints


#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool is_possible(unordered_map<int, int>& mpp, int mid)

{

    bool flag1 = false;

    bool flag2 = false;

    bool flag = false;

    for (auto it : mpp) {

        if (it.second >= mid) {

            flag1 = true;

            if (it.second > mid)

                flag2 = true;

        }

    }

  

    int dimension = mpp.dimension() - 1;

    if (flag2)

        dimension += 1;

  

    return flag1 && dimension >= mid;

}

  

int maximum_size(vector<int>& arr, int n)

{

  

    

    

    unordered_map<int, int> mpp;

    for (auto it : arr) {

        mpp[it]++;

    }

  

    

    int low = 0;

    int excessive = n / 2;

  

    

    int ans = 0;

  

    whereas (low <= excessive) {

  

        

        int mid = (low + excessive) >> 1;

  

        

        

        

        

        if (is_possible(mpp, mid)) {

  

            ans = mid;

            low = mid + 1;

        }

  

        

        

        

        

        

        else {

            excessive = mid - 1;

        }

    }

  

    

    return ans;

}

  

int primary()

{

    vector<int> arr = { 4, 2, 4, 1, 4, 3, 4 };

    int n = arr.dimension();

  

    

    cout << maximum_size(arr, n);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments