Open In App

Count array elements that can be maximized by adding any permutation of first N natural numbers

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] consisting of N integers, the task is to determine the total number of array elements that can become the maximum value in the array by adding any permutation of [1, N] to the corresponding value in the given array.

Examples:

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:
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}

Naive Approach: The simplest approach is to generate all possible permutations of the first N natural numbers. Now, for each element of the given array, check if by adding any of the permutations makes the current element the largest element of the resulting array or not. If found to be true, then increase the count and check for the next element.

Time Complexity: O(N*N!)
Auxiliary Space: O(N)

Efficient Approach: The above approach can be optimized using Greedy Approach to check if an array element can become maximum by adding any permutation or not. Follow the steps below to solve the above problem:

  1. Sort the given array arr[] in decreasing order.
  2. Initialize variables count and mark with 0.
  3. Traverse the given array by adding the smallest value i.e., 1 to the largest number, second smallest value to the second-largest number, and so on.
  4. Also, update the mark with the maximum value found till each index in the above step.
  5. Before updating the variable mark, check if arr[i] can become maximum when N is added in it, by comparing it with mark. If yes, increment the counter count by 1.
  6. After all the above steps, print the count.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
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




<script>
// javascript program for the
// above approach
 
// Function to get the count of values
// that can have the maximum value
function countMaximum(a, n)
{
  
    // Sort array in decreasing order
    a.sort();
    a.reverse();
  
    // Stores the answer
    let count = 0;
  
    // mark stores the maximum value
    // till each index i
    let mark = 0;
  
    for(let 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
    document.write(count);
}
  
// Driver Code
 
     // Given array arr[]
    let arr = [ 8, 9, 6 ];
  
    let N = arr.length;
  
    // Function call
    countMaximum(arr, N);
  
 // This code is contributed by avijitmonal1998.
</script>


Output: 

2

Time Complexity: O(N log N)
Auxiliary Space: O(N)



Last Updated : 28 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads