Maximize number of groups formed with size not smaller than its largest element
Last Updated :
09 Sep, 2022
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 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
// 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
# 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#
// 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
JavaScript
<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>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Maximize groups to be formed such that product of size of group with its minimum element is at least K Given an array, arr[] of length N, and an integer K. The value of the i-th element is arr[i]. The task is to find the maximum number of groups such that for each group the product of the number of elements in that group and the minimum element is at least K. Note: Every element should belong to exac
8 min read
Maximize the number of indices such that element is greater than element to its left Given an array arr[] of N integers, the task is to maximize the number of indices such that an element is greater than the element to its left, i.e. arr[i+1] > arr[i] after rearranging the array.Examples: Input: arr[] = {200, 100, 100, 200} Output: 2 Explanation: By arranging the array in followi
6 min read
Maximum number of teams that can be formed with given persons Given two integers N and M which denote the number of persons of Type1 and Type2 respectively. The task is to find the maximum number of teams that can be formed with these two types of persons. A team can contain either 2 persons of Type1 and 1 person of Type2 or 1 person of Type1 and 2 persons of
7 min read
Maximize the Sum of Minimum in each Group of size K Given an array nums[] of size N and a positive integer K, divide the elements of the array into N/K groups, each of size K such that the sum of the smallest elements in each group is maximized. Examples: Input: nums = {1,4,3,2}, K = 2Output: 4Explanation: All possible groupings (ignoring the orderin
8 min read
Maximum Unique Element in every subarray of size K Given an array and an integer K. We need to find the maximum of every segment of length K which has no duplicates in that segment. Examples: Input : a[] = {1, 2, 2, 3, 3}, K = 3. Output : 1 3 2 For segment (1, 2, 2), Maximum = 1. For segment (2, 2, 3), Maximum = 3. For segment (2, 3, 3), Maximum = 2
7 min read
Maximum difference between groups of size two Given an array of even number of elements, form groups of 2 using these array elements such that the difference between the group with highest sum and the one with lowest sum is maximum. Note: An element can be a part of one group only and it has to be a part of at least 1 group. Examples: Input : a
9 min read