Given an array arr[] of N positive integers(1 ? arr[i] ? N ), divide the elements of the array into groups such that the size of each group is greater than or equal to the largest element of that group. It may be also possible that an element cannot join any group. The task is to maximize the number of groups.Â
Examples:Â Â
Input: arr = {2, 3, 1, 2, 2}Â
Output: 2Â
Explanation:Â
In the first group we can take {1, 2}Â
In the second group we can take {2, 2, 3}Â
Therefore, the maximum 2 groups can be possible.Input: arr = {1, 1, 1}Â
Output: 3Â
Approach:Â
- Firstly store the number of occurrences of each element in an array.
- Now, Make groups of similar elements. For example: if there are three 1s in the array then make three groups for each 1.
- Then store the remaining elements and start grouping from the lowest element.
Below is the implementation of the above approach.Â
C++
// C++ implementation of above approachÂ
#include <bits/stdc++.h>using namespace std;Â
// Function that prints the number// of maximum groupsvoid makeGroups(int a[], int n){Â Â Â Â vector<int> v(n + 1, 0);Â
    // Store the number of    // occurrence of elements    for (int i = 0; i < n; i++) {        v[a[i]]++;    }Â
    int no_of_groups = 0;Â
    // Make all groups of similar    // elements and store the    // left numbers    for (int i = 1; i <= n; i++) {        no_of_groups += v[i] / i;Â
        v[i] = v[i] % i;    }Â
    int i = 1;    int total = 0;Â
    for (i = 1; i <= n; i++) {        // Condition for finding first        // leftover element        if (v[i] != 0) {            total = v[i];            break;        }    }Â
    i++;Â
    while (i <= n) {        // Condition for current        // leftover element        if (v[i] != 0) {            total += v[i];Â
            // Condition if group size            // is equal to or more than            // current element            if (total >= i) {                int rem = total - i;                no_of_groups++;                total = rem;            }        }        i++;    }Â
    // Printing maximum    // number of groups    cout << no_of_groups << "\n";}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 2, 3, 1, 2, 2 };Â
    int size = sizeof(arr) / sizeof(arr[0]);Â
    makeGroups(arr, size);Â
    return 0;} |
Java
// Java implementation of above approachimport java.util.*;class GFG{Â
// Function that prints the number// of maximum groupsstatic void makeGroups(int a[], int n){Â Â Â Â int []v = new int[n + 1];Â
    // Store the number of    // occurrence of elements    for (int i = 0; i < n; i++)    {        v[a[i]]++;    }Â
    int no_of_groups = 0;Â
    // Make all groups of similar    // elements and store the    // left numbers    for (int i = 1; i <= n; i++)     {        no_of_groups += v[i] / i;Â
        v[i] = v[i] % i;    }Â
    int i = 1;    int total = 0;Â
    for (i = 1; i <= n; i++)     {        // Condition for finding first        // leftover element        if (v[i] != 0)        {            total = v[i];            break;        }    }Â
    i++;Â
    while (i <= n)     {        // Condition for current        // leftover element        if (v[i] != 0)         {            total += v[i];Â
            // Condition if group size            // is equal to or more than            // current element            if (total >= i)             {                int rem = total - i;                no_of_groups++;                total = rem;            }        }        i++;    }Â
    // Printing maximum    // number of groups    System.out.print(no_of_groups + "\n");}Â
// Driver Codepublic static void main(String[] args){Â Â Â Â int arr[] = { 2, 3, 1, 2, 2 };Â
    int size = arr.length;Â
    makeGroups(arr, size);}}Â
// This code is contributed by sapnasingh4991 |
Python3
# python3 implementation of above approach# Function that prints the number# of maximum groupsdef makeGroups(a, n):Â Â Â Â v = [0] * (n + 1)Â
    # Store the number of    # occurrence of elements    for i in range (n):        v[a[i]] += 1        no_of_groups = 0Â
    # Make all groups of similar    # elements and store the    # left numbers    for i in range (1, n + 1):        no_of_groups += v[i] // i        v[i] = v[i] % i        i = 1    total = 0    for i in range ( 1, n + 1):               # Condition for finding first        # leftover element        if (v[i] != 0):            total = v[i]            breakÂ
    i += 1    while (i <= n):               # Condition for current        # leftover element        if (v[i] != 0):            total += v[i]Â
            # Condition if group size            # is equal to or more than            # current element            if (total >= i):                rem = total - i                no_of_groups += 1                total = rem                 i += 1Â
    # Printing maximum    # number of groups    print (no_of_groups)Â
# Driver Codeif __name__ == "__main__":Â Â Â Â Â arr = [2, 3, 1, 2, 2]Â Â Â Â size = len(arr)Â Â Â Â makeGroups(arr, size)Â
# This code is contributed by Chitranayal |
C#
// C# implementation of above approachusing System;class GFG{Â
// Function that prints the number// of maximum groupsstatic void makeGroups(int []a, int n){    int []v = new int[n + 1];    int i = 0;         // Store the number of    // occurrence of elements    for(i = 0; i < n; i++)    {       v[a[i]]++;    }Â
    int no_of_groups = 0;Â
    // Make all groups of similar    // elements and store the    // left numbers    for(i = 1; i <= n; i++)     {       no_of_groups += v[i] / i;       v[i] = v[i] % i;    }Â
    i = 1;    int total = 0;    for(i = 1; i <= n; i++)     {                // Condition for finding first       // leftover element       if (v[i] != 0)       {           total = v[i];           break;       }    }    i++;Â
    while (i <= n)     {                 // Condition for current        // leftover element        if (v[i] != 0)         {            total += v[i];Â
            // Condition if group size            // is equal to or more than            // current element            if (total >= i)             {                int rem = total - i;                no_of_groups++;                total = rem;            }        }        i++;    }Â
    // Printing maximum    // number of groups    Console.Write(no_of_groups + "\n");}Â
// Driver Codepublic static void Main(String[] args){Â Â Â Â int []arr = { 2, 3, 1, 2, 2 };Â Â Â Â int size = arr.Length;Â
    makeGroups(arr, size);}}Â
// This code is contributed by sapnasingh4991 |
Javascript
<script>Â
// Javascript implementation of above approachÂ
// Function that prints the number// of maximum groupsfunction makeGroups(a, n){    let v = Array.from({length: n+1}, (_, i) => 0);      // Store the number of    // occurrence of elements    for (let i = 0; i < n; i++)    {        v[a[i]]++;    }      let no_of_groups = 0;      // Make all groups of similar    // elements and store the    // left numbers    for (let i = 1; i <= n; i++)    {        no_of_groups += Math.floor(v[i] / i);          v[i] = v[i] % i;    }      let i = 1;    let total = 0;      for (i = 1; i <= n; i++)    {        // Condition for finding first        // leftover element        if (v[i] != 0)        {            total = v[i];            break;        }    }      i++;      while (i <= n)    {        // Condition for current        // leftover element        if (v[i] != 0)        {            total += v[i];              // Condition if group size            // is equal to or more than            // current element            if (total >= i)            {                let rem = total - i;                no_of_groups++;                total = rem;            }        }        i++;    }      // Printing maximum    // number of groups    document.write(no_of_groups + "\n");}Â
// Driver Code        let arr = [ 2, 3, 1, 2, 2 ];      let size = arr.length;      makeGroups(arr, size);            </script> |
2
Â
Time Complexity: O(N)Â
Auxiliary Space: O(N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
