给定一个由N个整数组成的数组arr [] ,任务是通过将[1,N]的任何置换添加到给定数组中的相应值来确定可以成为数组中最大值的数组元素总数。
例子:
Input: N = 3, arr[] = {8, 9, 6}
Output: 2
Explanation:
Following permutations can be added to get maximum values:
For index 0 to be maximum, add {3, 1, 2}. Therefore, arr[] = {8 + 3, 9 + 1, 6 + 2} = {11, 10, 8}
For index 1 to be maximum, add {1, 3, 2}. Therefore, arr[] = {8 + 1, 9 + 3, 6 + 2} = {9, 12, 8}
For index 2 to be maximum, there is no possible permutation such that arr[2] becomes maximum.
Input: N = 5 arr[] = {15, 14, 15, 12, 14}
Output: 4
Explanation:
Following permutations can be added to get maximum values:
For index 0 to be maximum, add {5, 4, 3, 2, 1}. Therefore, arr[] = {15+5, 14+4, 15+3, 12+2, 14+1} = {20, 18, 18, 14, 15}
For index 1 to be maximum, add {1, 5, 4, 3, 2}. Therefore, arr[] = {15+1, 14+5, 15+4, 12+3, 14+2} = {16, 19, 19, 15, 16}
For index 2 to be maximum, add {1, 5, 4, 3, 2}. Therefore, arr[] = {15+1, 14+5, 15+4, 12+3, 14+2} = {16, 19, 19, 15, 16}
For index 3 to be maximum, there is no possible permutation such that arr[3] becomes maximum.
For index 4 to be maximum, add {1, 2, 3, 4, 5}. Therefore, arr[] = {15+1, 14+2, 15+3, 12+4, 14+5} = {16, 16, 18, 16, 19}
天真的方法:最简单的方法是生成前N个自然数的所有可能排列。现在,对于给定数组的每个元素,检查是否通过添加任何排列使当前元素成为结果数组中最大的元素。如果发现是真的,则增加计数并检查下一个元素。
时间复杂度: O(N * N!)
辅助空间: O(N)
高效方法:可以使用贪婪方法对上述方法进行优化,以检查是否可以通过添加任何排列来使数组元素变为最大。请按照以下步骤解决以上问题:
- 以降序对给定数组arr []进行排序。
- 初始化变量count并用0标记。
- 通过将最小值(即1)加到最大数上,将第二个最小值加到第二大数上来遍历给定数组,依此类推。
- 另外,以在上述步骤中的每个索引之前找到的最大值更新标记。
- 在更新变量mark之前,通过将其与mark进行比较,检查添加了N的arr [i]是否可以变为最大值。如果是,则将计数器计数增加1 。
- 完成上述所有步骤后,打印计数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Comparator function to sort the
// given array
bool cmp(int x, int y) { return x > y; }
// Function to get the count of values
// that can have the maximum value
void countMaximum(int* a, int n)
{
// Sort array in decreasing order
sort(a, a + n, cmp);
// Stores the answer
int count = 0;
// mark stores the maximum value
// till each index i
int mark = 0;
for (int i = 0; i < n; ++i) {
// Check if arr[i] can be maximum
if ((a[i] + n >= mark)) {
count += 1;
}
// Update the mark
mark = max(mark, a[i] + i + 1);
}
// Print the total count
cout << count;
}
// Driver Code
int main()
{
// Given array arr[]
int arr[] = { 8, 9, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function Call
countMaximum(arr, N);
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to get the count of values
// that can have the maximum value
static void countMaximum(Integer []a, int n)
{
// Sort array in decreasing order
Arrays.sort(a, Collections.reverseOrder());
// Stores the answer
int count = 0;
// mark stores the maximum value
// till each index i
int mark = 0;
for(int i = 0; i < n; ++i)
{
// Check if arr[i] can be maximum
if ((a[i] + n >= mark))
{
count += 1;
}
// Update the mark
mark = Math.max(mark, a[i] + i + 1);
}
// Print the total count
System.out.print(count);
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
Integer arr[] = { 8, 9, 6 };
int N = arr.length;
// Function call
countMaximum(arr, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
# Function to get the count of values
# that can have the maximum value
def countMaximum(a, n):
# Sort array in decreasing order
a.sort(reverse = True);
# Stores the answer
count = 0;
# mark stores the maximum value
# till each index i
mark = 0;
for i in range(n):
# Check if arr[i] can be maximum
if ((a[i] + n >= mark)):
count += 1;
# Update the mark
mark = max(mark, a[i] + i + 1);
# Print the total count
print(count);
# Driver Code
if __name__ == '__main__':
# Given array arr
arr = [ 8, 9, 6 ];
N = len(arr);
# Function call
countMaximum(arr, N);
# This code is contributed by Amit Katiyar
C#
// C# program for the above approach
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
class GFG{
// Function to get the count of values
// that can have the maximum value
static void countMaximum(int []a, int n)
{
// Sort array in decreasing order
a.OrderByDescending(c => c).ToArray();
// Stores the answer
int count = 0;
// mark stores the maximum value
// till each index i
int mark = 0;
for(int i = 0; i < n; ++i)
{
// Check if arr[i] can be maximum
if ((a[i] + n >= mark))
{
count += 1;
}
// Update the mark
mark = Math.Max(mark, a[i] + i + 1);
}
// Print the total count
Console.Write(count);
}
// Driver Code
public static void Main(string[] args)
{
// Given array arr[]
int []arr = { 8, 9, 6 };
int N = arr.Length;
// Function call
countMaximum(arr, N);
}
}
// This code is contributed by rutvik_56
2
时间复杂度: O(N log N)
辅助空间: O(N)