Verify if Array might be divided into Okay teams of measurement C and all components of group are distinct


Given an array A[] of measurement N, and two integers Okay and C, the duty is to verify if all the weather of the array might be grouped into Okay teams such that every group has at most C components and all the weather of a gaggle are distinct.

Examples:

Enter: N=6, A=[1, 2, 1, 2, 3, 4], Okay = 2, C=3
Output: Doable
Clarification: Let 2 teams be k1, k2 of capability 3.
If we put component with constructive integer 1, 2, 3 on group k1 i.e k1 have [1, 2, 3] and 
if we put remaining 3 components 1, 2, 4 on group k2 i.e k2 have [1, 2, 4] then it attainable
to place all of the balls .

Enter: N = 8, Balls = [1, 1, 1, 1, 1, 2, 2, 2], Okay = 2, C = 4
Output: Not possible
Clarification: It isn’t attainable to place the weather into
2 teams such that every group has all distinctive components

 Method: The issue might be solved primarily based on the next thought:

Retailer the frequency depend of every component. If any component has frequency depend of greater than Okay then the reply can’t exist.

Comply with the steps talked about under to implement the concept.

  • Declare a map.
  • Retailer the frequency of each component contained in the map.
  • Iterate over the map and verify if any component has a frequency higher than Okay then print the unattainable and return.
  • Verify if N > Okay*C then print Not possible and return.
  • In any other case, the reply is feasible.

Under is the Implementation of the strategy 

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool Verify(int N, vector<int>& Balls, int Okay, int C)

{

    

    map<int, int> freq;

    for (auto i : Balls)

  

        

        freq[i]++;

  

    for (auto i : freq) {

  

        

        if (i.second > Okay) {

            return false;

        }

    }

  

    

    

    if (N > Okay * C)

        return false;

    return true;

}

  

int foremost()

{

    int N = 8;

    vector<int> Balls = { 1, 1, 1, 1, 1, 2, 2, 2 };

  

    int Okay = 2;

    int C = 4;

  

    

    if (Verify(N, Balls, Okay, C)) {

        cout << "Doable" << endl;

    }

    else {

        cout << "Not possible" << endl;

    }

    return 0;

}

Time Complexity: O(N log (N))
Auxiliary House: O(N)

House Optimized Method:

The house requirement of the issue might be diminished. The array might be sorted so the frequency of every component might be counted with out the utilization of additional map as a result of they may all be current in consecutive indices.

Comply with the steps talked about under to implement the concept:

  • Kind the array in ascending order.
  • Keep a Depend variable that can depend the frequency of the current component.
  • If the frequency of any component is larger than Okay, Print “Not possible” and return;
  • Verify if N > Okay*C then print Not possible and return.
  • In any other case, a solution is feasible.

Under is the implementation of the above strategy

C++

  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

bool Verify(int N, vector<int>& Balls, int Okay, int C)

{

    

    kind(Balls.start(), Balls.finish());

  

    

    int cnt = 1;

    for (int i = 0; i < N - 1; i++) {

        if (Balls[i] != Balls[i - 1]) {

            if (cnt > Okay)

                return false;

            cnt = 1;

        }

        else

            

            

            cnt++;

    }

    

    

    if (N > Okay * C)

        return false;

    return true;

}

  

int foremost()

{

    int N = 8;

    vector<int> Balls = { 1, 1, 1, 1, 1, 2, 2, 2 };

  

    int Okay = 2;

    int C = 4;

  

    

    if (Verify(N, Balls, Okay, C)) {

        cout << "Doable" << endl;

    }

    else {

        cout << "Not possible" << endl;

    }

    return 0;

}

Time Complexity: O(N * logN) the time taken for sorting is O(N * logN)
Auxiliary House: O(1)

Leave a Reply