Count array elements that can be maximized by adding any permutation of first N natural numbers
Last Updated :
28 Feb, 2022
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: 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}
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:
- Sort the given array arr[] in decreasing order.
- Initialize variables count and mark with 0.
- 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.
- Also, update the mark with the maximum value found till each index in the above step.
- 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.
- After all the above steps, print the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool cmp( int x, int y) { return x > y; }
void countMaximum( int * a, int n)
{
sort(a, a + n, cmp);
int count = 0;
int mark = 0;
for ( int i = 0; i < n; ++i) {
if ((a[i] + n >= mark)) {
count += 1;
}
mark = max(mark, a[i] + i + 1);
}
cout << count;
}
int main()
{
int arr[] = { 8, 9, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
countMaximum(arr, N);
}
|
Java
import java.util.*;
class GFG{
static void countMaximum(Integer []a, int n)
{
Arrays.sort(a, Collections.reverseOrder());
int count = 0 ;
int mark = 0 ;
for ( int i = 0 ; i < n; ++i)
{
if ((a[i] + n >= mark))
{
count += 1 ;
}
mark = Math.max(mark, a[i] + i + 1 );
}
System.out.print(count);
}
public static void main(String[] args)
{
Integer arr[] = { 8 , 9 , 6 };
int N = arr.length;
countMaximum(arr, N);
}
}
|
Python3
def countMaximum(a, n):
a.sort(reverse = True );
count = 0 ;
mark = 0 ;
for i in range (n):
if ((a[i] + n > = mark)):
count + = 1 ;
mark = max (mark, a[i] + i + 1 );
print (count);
if __name__ = = '__main__' :
arr = [ 8 , 9 , 6 ];
N = len (arr);
countMaximum(arr, N);
|
C#
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
class GFG{
static void countMaximum( int []a, int n)
{
a.OrderByDescending(c => c).ToArray();
int count = 0;
int mark = 0;
for ( int i = 0; i < n; ++i)
{
if ((a[i] + n >= mark))
{
count += 1;
}
mark = Math.Max(mark, a[i] + i + 1);
}
Console.Write(count);
}
public static void Main( string [] args)
{
int []arr = { 8, 9, 6 };
int N = arr.Length;
countMaximum(arr, N);
}
}
|
Javascript
<script>
function countMaximum(a, n)
{
a.sort();
a.reverse();
let count = 0;
let mark = 0;
for (let i = 0; i < n; ++i)
{
if ((a[i] + n >= mark))
{
count += 1;
}
mark = Math.max(mark, a[i] + i + 1);
}
document.write(count);
}
let arr = [ 8, 9, 6 ];
let N = arr.length;
countMaximum(arr, N);
</script>
|
Time Complexity: O(N log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...