给定一个由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进行比较,检查arr[i]在添加N 时是否可以达到最大值。如果是,将计数器计数增加1 。
- 完成上述所有步骤后,打印count 。
下面是上述方法的实现:
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
Javascript
2
时间复杂度: O(N log N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。