Count the maximum number of elements that can be selected from the array
Given an array arr[], the task is to count the maximum number of elements that can be selected from the given array following the below selection process:
- At 1st selection, select an element which is greater than or equal to 1.
- At 2nd selection, select an element which is greater than or equal to 2.
- At 3rd selection, select an element which is greater than or equal to 3 and so on.
An element can be selected only once. The operation stops when it is not possible to select any element. So, the task is to maximize the count of selection from the array.
Examples:
Input : arr[] = { 4, 1, 3, 1 }
Output : 3
1st Selection: 1 is selected as 1 >= 1.
2nd Selection: 3 is selected as 3 >= 2.
3rd Selection: 4 is selected as 4 >= 3.
No more selections are possible. Therefore, the answers is 3.
Input : arr[] = { 2, 1, 1, 2, 1 }
Output : 2
Approach: In order to maximize the count of selection it is necessary to select the smallest possible numbers first and then the bigger numbers if the selection is not possible. This can be done easily by sorting the array. Now, loop through the array and increment the result by 1 when the element is greater than or equal to the number to select for the current operation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSelectionCount( int a[], int n)
{
int res = 0;
sort(a, a + n);
int select = 1;
for ( int i = 0; i < n; i++) {
if (a[i] >= select) {
res++;
select++;
}
}
return res;
}
int main()
{
int arr[] = { 4, 2, 1, 3, 5, 1, 4 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << maxSelectionCount(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxSelectionCount( int a[], int n)
{
int res = 0 ;
Arrays.sort(a);
int select = 1 ;
for ( int i = 0 ; i < n; i++)
{
if (a[i] >= select)
{
res++;
select++;
}
}
return res;
}
public static void main(String[] args)
{
int arr[] = { 4 , 2 , 1 , 3 , 5 , 1 , 4 };
int N = arr.length;
System.out.println(maxSelectionCount(arr, N));
}
}
|
Python3
def maxSelectionCount(a, n):
res = 0 ;
a.sort();
select = 1 ;
for i in range (n):
if (a[i] > = select):
res + = 1 ;
select + = 1 ;
return res;
arr = [ 4 , 2 , 1 , 3 , 5 , 1 , 4 ];
N = len (arr);
print (maxSelectionCount(arr, N));
|
C#
using System;
class GFG
{
static int maxSelectionCount( int []a, int n)
{
int res = 0;
Array.Sort(a);
int select = 1;
for ( int i = 0; i < n; i++)
{
if (a[i] >= select )
{
res++;
select ++;
}
}
return res;
}
public static void Main()
{
int []arr = {4, 2, 1, 3, 5, 1, 4};
int N = arr.Length;
Console.WriteLine(maxSelectionCount(arr, N));
}
}
|
Javascript
<script>
function maxSelectionCount(a, n)
{
var res = 0;
a.sort();
var select = 1;
for ( var i = 0; i < n; i++)
{
if (a[i] >= select)
{
res++;
select++;
}
}
return res;
}
var arr = [ 4, 2, 1, 3, 5, 1, 4 ];
var N = arr.length;
document.write(maxSelectionCount(arr, N));
</script>
|
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Last Updated :
14 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...