Maximize number of groups formed with size not smaller than its largest element

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 occurrence 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 remaining elements and start grouping from the lowest element.

Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function that prints the number
// of maximum groups
void 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 Code
int main()
{
    int arr[] = { 2, 3, 1, 2, 2 };
  
    int size = sizeof(arr) / sizeof(arr[0]);
  
    makeGroups(arr, size);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
import java.util.*;
class GFG{
  
// Function that prints the number
// of maximum groups
static 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 Code
public 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System;
class GFG{
  
// Function that prints the number
// of maximum groups
static 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 Code
public 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

chevron_right


Output:

2

Time Complexity: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : sapnasingh4991