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++ 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;
} |
// 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 |
# python3 implementation of above approach # Function that prints the number # of maximum groups def 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 Code if __name__ = = "__main__" :
arr = [ 2 , 3 , 1 , 2 , 2 ]
size = len (arr)
makeGroups(arr, size)
# This code is contributed by Chitranayal |
// 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 |
<script> // Javascript implementation of above approach // Function that prints the number // of maximum groups function 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)