Given an array arr[] of size N, the task is to rearrange the array elements such that the sum of MEX of all prefix arrays is the maximum possible.
Note: MEX of a sequence is the minimum non-negative number not present in the sequence.
Examples:
Input: arr[] = {2, 0, 1}
Output: 0, 1, 2
Explanation:
Sum of MEX of all prefix arrays of all possible permutations of the given array:
arr[] = {0, 1, 2}, Mex(0) + Mex(0, 1) + Mex(0, 1, 2) = 1 + 2 + 3 = 6
arr[] = {1, 0, 2}, Mex(1) + Mex(1, 0) + Mex(1, 0, 2) = 0 + 2 + 3 = 5
arr[] = {2, 0, 1}, Mex(2) + Mex(2, 0) + Mex(2, 0, 1) = 0 + 1 + 3 = 4
arr[] = {0, 2, 1}, Mex(0) + Mex(0, 2) + Mex(0, 2, 1) = 1 + 1 + 3 = 5
arr[] = {1, 2, 0}, Mex(1) + Mex(1, 2) + Mex(1, 2, 0) = 0 + 0 + 3 = 3
arr[] = {2, 1, 0}, Mex(2) + Mex(2, 1) + Mex(2, 1, 0) = 0 + 0 + 3 = 3
Hence, the maximum sum possible is 6.
Input: arr[] = {1, 0, 0}
Output: 0, 1, 0
Explanation:
Sum of MEX of all prefix arrays of all possible permutations of the given array:
arr[]={1, 0, 0}, Mex(1) + Mex(1, 0) + Mex(1, 0, 0) = 0 + 2 + 2 = 4
arr[]={0, 1, 0}, Mex(0) + Mex(0, 1) + Mex(0, 1, 0) = 1 + 2 + 2 = 5
arr[]={0, 0, 1}, Mex(0) + Mex(0, 0) + Mex(0, 0, 1) = 1 + 1 + 2 = 4
Hence, the maximum value is 5 for the arrangement, arr[]={0, 1, 0}.
Naive Approach: The simplest approach is to generate all possible permutations of the given array arr[] and then for each permutation, find the value of MEX of all the prefix arrays, while keeping track of the overall maximum value. After iterating over all possible permutations, print the permutation having the largest value.
Time Complexity: O(N2 * N!)
Auxiliary Space: O(N)
Efficient Approach: The optimal idea is based on the observation that the sum of MEX of prefix arrays will be maximum when all the distinct elements are arranged in increasing order and the duplicates are present at the end of the array.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maximumMex( int arr[], int N)
{
vector< int > ans;
sort(arr, arr + N);
for ( int i = 0; i < N; i++) {
if (i == 0 || arr[i] != arr[i - 1])
ans.push_back(arr[i]);
}
for ( int i = 0; i < N; i++) {
if (i > 0 && arr[i] == arr[i - 1])
ans.push_back(arr[i]);
}
for ( int i = 0; i < N; i++)
cout << ans[i] << " " ;
}
int main()
{
int arr[] = { 1, 0, 0 };
int N = sizeof (arr) / sizeof (arr[0]);
maximumMex(arr, N);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static void maximumMex( int arr[], int N)
{
int ans[] = new int [ 2 * N];
Arrays.sort(ans);
int j = 0 ;
for ( int i = 0 ; i < N; i++)
{
if (i == 0 || arr[i] != arr[i - 1 ])
{
j += 1 ;
ans[j] = arr[i];
}
}
for ( int i = 0 ; i < N; i++)
{
if (i > 0 && arr[i] == arr[i - 1 ])
{
j += 1 ;
ans[j] = arr[i];
}
}
for ( int i = 0 ; i < N; i++)
System.out.print(ans[i] + " " );
}
public static void main (String[] args)
{
int arr[] = { 1 , 0 , 0 };
int N = arr.length;
maximumMex(arr, N);
}
}
|
Python3
def maximumMex(arr, N):
ans = []
arr = sorted (arr)
for i in range (N):
if (i = = 0 or arr[i] ! = arr[i - 1 ]):
ans.append(arr[i])
for i in range (N):
if (i > 0 and arr[i] = = arr[i - 1 ]):
ans.append(arr[i])
for i in range (N):
print (ans[i], end = " " )
if __name__ = = '__main__' :
arr = [ 1 , 0 , 0 ]
N = len (arr)
maximumMex(arr, N)
|
C#
using System;
class GFG{
static void maximumMex( int []arr, int N)
{
int []ans = new int [2 * N];
Array.Sort(ans);
int j = 0;
for ( int i = 0; i < N; i++)
{
if (i == 0 || arr[i] != arr[i - 1])
{
j += 1;
ans[j] = arr[i];
}
}
for ( int i = 0; i < N; i++)
{
if (i > 0 && arr[i] == arr[i - 1])
{
j += 1;
ans[j] = arr[i];
}
}
for ( int i = 0; i < N; i++)
Console.Write(ans[i] + " " );
}
public static void Main ( string [] args)
{
int []arr = { 1, 0, 0 };
int N = arr.Length;
maximumMex(arr, N);
}
}
|
Javascript
<script>
function maximumMex(arr, N)
{
var ans = [];
arr.sort();
var i;
for (i = 0; i < N; i++) {
if (i == 0 || arr[i] != arr[i - 1])
ans.push(arr[i]);
}
for (i = 0; i < N; i++) {
if (i > 0 && arr[i] == arr[i - 1])
ans.push(arr[i]);
}
for (i = 0; i < N; i++)
document.write(ans[i] + " " );
}
var arr = [1, 0, 0];
var N = arr.length;
maximumMex(arr, N);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)