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 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++
#include <bits/stdc++.h>
using namespace std;
void makeGroups( int a[], int n)
{
vector< int > v(n + 1, 0);
for ( int i = 0; i < n; i++) {
v[a[i]]++;
}
int no_of_groups = 0;
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++) {
if (v[i] != 0) {
total = v[i];
break ;
}
}
i++;
while (i <= n) {
if (v[i] != 0) {
total += v[i];
if (total >= i) {
int rem = total - i;
no_of_groups++;
total = rem;
}
}
i++;
}
cout << no_of_groups << "\n" ;
}
int main()
{
int arr[] = { 2, 3, 1, 2, 2 };
int size = sizeof (arr) / sizeof (arr[0]);
makeGroups(arr, size);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void makeGroups( int a[], int n)
{
int []v = new int [n + 1 ];
for ( int i = 0 ; i < n; i++)
{
v[a[i]]++;
}
int no_of_groups = 0 ;
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++)
{
if (v[i] != 0 )
{
total = v[i];
break ;
}
}
i++;
while (i <= n)
{
if (v[i] != 0 )
{
total += v[i];
if (total >= i)
{
int rem = total - i;
no_of_groups++;
total = rem;
}
}
i++;
}
System.out.print(no_of_groups + "\n" );
}
public static void main(String[] args)
{
int arr[] = { 2 , 3 , 1 , 2 , 2 };
int size = arr.length;
makeGroups(arr, size);
}
}
|
Python3
def makeGroups(a, n):
v = [ 0 ] * (n + 1 )
for i in range (n):
v[a[i]] + = 1
no_of_groups = 0
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 ):
if (v[i] ! = 0 ):
total = v[i]
break
i + = 1
while (i < = n):
if (v[i] ! = 0 ):
total + = v[i]
if (total > = i):
rem = total - i
no_of_groups + = 1
total = rem
i + = 1
print (no_of_groups)
if __name__ = = "__main__" :
arr = [ 2 , 3 , 1 , 2 , 2 ]
size = len (arr)
makeGroups(arr, size)
|
C#
using System;
class GFG{
static void makeGroups( int []a, int n)
{
int []v = new int [n + 1];
int i = 0;
for (i = 0; i < n; i++)
{
v[a[i]]++;
}
int no_of_groups = 0;
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++)
{
if (v[i] != 0)
{
total = v[i];
break ;
}
}
i++;
while (i <= n)
{
if (v[i] != 0)
{
total += v[i];
if (total >= i)
{
int rem = total - i;
no_of_groups++;
total = rem;
}
}
i++;
}
Console.Write(no_of_groups + "\n" );
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 1, 2, 2 };
int size = arr.Length;
makeGroups(arr, size);
}
}
|
Javascript
<script>
function makeGroups(a, n)
{
let v = Array.from({length: n+1}, (_, i) => 0);
for (let i = 0; i < n; i++)
{
v[a[i]]++;
}
let no_of_groups = 0;
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++)
{
if (v[i] != 0)
{
total = v[i];
break ;
}
}
i++;
while (i <= n)
{
if (v[i] != 0)
{
total += v[i];
if (total >= i)
{
let rem = total - i;
no_of_groups++;
total = rem;
}
}
i++;
}
document.write(no_of_groups + "\n" );
}
let arr = [ 2, 3, 1, 2, 2 ];
let size = arr.length;
makeGroups(arr, size);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
09 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...