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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the maximum count of // selection possible from the given array // following the given process int maxSelectionCount(int a[], int n) { // Initialize result int res = 0; // Sorting the array sort(a, a + n); // Initialize the select variable int select = 1; // Loop through array for (int i = 0; i < n; i++) { // If selection is possible if (a[i] >= select) { res++; // Increment result select++; // Increment selection variable } } return res; } // Driver Code 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
// Java implementation of the approach import java.util.*; class GFG { // Function to return the maximum count of // selection possible from the given array // following the given process static int maxSelectionCount(int a[], int n) { // Initialize result int res = 0; // Sorting the array Arrays.sort(a); // Initialize the select variable int select = 1; // Loop through array for (int i = 0; i < n; i++) { // If selection is possible if (a[i] >= select) { res++; // Increment result select++; // Increment selection variable } } return res; } // Driver Code 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)); } } // This code contributed by Rajput-Ji |
Python3
# Python implementation of the approach # Function to return the maximum count of # selection possible from the given array # following the given process def maxSelectionCount(a, n): # Initialize result res = 0; # Sorting the array a.sort(); # Initialize the select variable select = 1; # Loop through array for i in range(n): # If selection is possible if (a[i] >= select): res += 1; # Increment result select += 1; # Increment selection variable return res; # Driver Code arr = [ 4, 2, 1, 3, 5, 1, 4 ]; N = len(arr); print(maxSelectionCount(arr, N)); # This code contributed by PrinciRaj1992 |
C#
// C# implementation of the approach using System; class GFG { // Function to return the maximum count of // selection possible from the given array // following the given process static int maxSelectionCount(int []a, int n) { // Initialize result int res = 0; // Sorting the array Array.Sort(a); // Initialize the select variable int select = 1; // Loop through array for (int i = 0; i < n; i++) { // If selection is possible if (a[i] >= select) { res++; // Increment result select++; // Increment selection variable } } return res; } // Driver Code public static void Main() { int []arr = {4, 2, 1, 3, 5, 1, 4}; int N = arr.Length; Console.WriteLine(maxSelectionCount(arr, N)); } } // This code contributed by AnkitRai01 |
5
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.
Recommended Posts:
- Maximum number of unique Triplets such that each element is selected only once
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Check if the array can be sorted only if the elements on given positions can be swapped
- Check if Array elements can be maximized upto M by adding all elements from another array
- Check whether an array can be fit into another array rearranging the elements in the array
- Maximum in an array that can make another array sorted
- Check if an Array can be Sorted by picking only the corner Array elements
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Longest subarray whose elements can be made equal by maximum K increments
- Maximum items that can be bought from the cost Array based on given conditions
- Maximum number of partitions that can be sorted individually to make sorted
- Maximum number of segments that can contain the given points
- Maximum number of elements greater than X after equally distributing subset of array
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Check if elements of array can be arranged in AP, GP or HP
- Check if an array can be split into subsets of K consecutive elements
- Count of elements in first Array greater than second Array with each element considered only once
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Count elements such that there are exactly X elements with values greater than or equal to X
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
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.

