📌  相关文章
📜  通过添加前N个自然数的任意排列来计数可以最大化的数组元素

📅  最后修改于: 2021-04-28 18:13:02             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是通过将[1,N]的任何置换添加到给定数组中的相应值来确定可以成为数组中最大值的数组元素总数。

例子:

天真的方法:最简单的方法是生成前N个自然数的所有可能排列。现在,对于给定数组的每个元素,检查是否通过添加任何排列使当前元素成为结果数组中最大的元素。如果发现是真的,则增加计数并检查下一个元素。

时间复杂度: O(N * N!)
辅助空间: O(N)

高效方法:可以使用贪婪方法对上述方法进行优化,以检查是否可以通过添加任何排列来使数组元素变为最大。请按照以下步骤解决以上问题:

  1. 以降序对给定数组arr []进行排序。
  2. 初始化变量count并用0标记
  3. 通过将最小值(即1)加到最大数上,将第二个最小值加到第二大数上来遍历给定数组,依此类推。
  4. 另外,以在上述步骤中的每个索引之前找到的最大值更新标记
  5. 在更新变量mark之前,通过将其与mark进行比较,检查添加了N的arr [i]是否可以变为最大值。如果是,则将计数器计数增加1
  6. 完成上述所有步骤后,打印计数

下面是上述方法的实现:

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)