给定数组arr [] ,任务是按照以下选择过程计算可以从给定数组中选择的最大元素数:
- 在第一个选择中,选择一个大于或等于1的元素。
- 在第二选择中,选择一个大于或等于2的元素。
- 在第3个选择中,选择一个大于或等于3的元素,依此类推。
一个元素只能选择一次。当无法选择任何元素时,操作停止。因此,任务是最大化从阵列中进行选择的次数。
例子:
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
方法:为了最大程度地增加选择的数量,有必要先选择尽可能小的数字,然后再选择更大的数字(如果无法选择)。通过对数组进行排序,可以轻松完成此操作。现在,遍历数组,当元素大于或等于要为当前操作选择的数字时,将结果加1。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
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