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++
// 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 processint 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 Codeint 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 approachimport 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 processdef 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 Codearr = [ 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 |
Javascript
<script>Â
// Javascript implementation of the approachÂ
// Function to return the maximum count of // selection possible from the given array // following the given processfunction maxSelectionCount(a, n){         // Initialize result    var res = 0;Â
    // Sorting the array    a.sort();Â
    // Initialize the select variable    var select = 1;Â
    // Loop through array    for(var i = 0; i < n; i++)     {                 // If selection is possible        if (a[i] >= select)        {            // Increment result            res++;                          // Increment selection variable            select++;         }    }    return res;}Â
// Driver Codevar arr = [ 4, 2, 1, 3, 5, 1, 4 ];var N = arr.length;Â
document.write(maxSelectionCount(arr, N));Â
// This code is contributed by rrrtnxÂ
</script> |
5
Â
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)Â
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



