Rearrange Array to maximize sum of MEX of all Subarrays starting from first index
Last Updated :
31 Oct, 2023
Given an array arr[] of N elements ranging from 0 to N-1(both included), the task is to rearrange the array such that the sum of all the MEX from every subarray starting from index 0 is maximum. Duplicates can be present in the array.
MEX of an array is the first non-negative element that is missing from the array.
Examples:
Input: N = 3, arr[] = {2, 1, 0}
Output: 6
Explanation: Initially for subarray {2} the missing element is 0.
For {2, 1} the missing element is 0, and for {2, 1, 0} the missing element is 3.
In this way the answer is 0 + 0 + 3 = 3.
But if the array is rearranged like {0, 1, 2}, the answer will be 1 + 2 + 3 = 6.
Input: N = 5, arr[] = {0, 0, 0, 0, 0}
Output: 5
Approach:
The idea is to put the smaller elements in the beginning of new array to get highest possible missing whole numbers for each subarray, but if we have duplicates then the duplicates would be inserted in the end of the new array.
Follow the steps to solve the problems:
- Sort the array.
- Initialize 2 variables cur to store the current smallest missing whole number and count to store the number of times the smallest whole number would be smallest for the next subarrays.
- If the current element is greater than cur, then for all the next subarrays the smallest whole number would be cur, as the array is sorted. So add the number of remaining subarrays to count and break the loop.
- Else if we find the duplicates then increase the count variable to add the value in the end.
- Else, for each index update cur and add the values into an ans variable that will contain the final answer.
- In the end, if cur is not 0, means there are subarrays that have cur as the smallest whole number and that are not added to ans, so add cur * count to the ans and return it.
Illustration:
Consider N = 6, arr[] = {4, 2, 0, 4, 1, 0}
Initialize, ans = 0, cur = 0, count = 0
Sort the array, so arr[] = {0, 0, 1, 2, 4, 4}
For index 0:
=> arr[i] = cur,
=> thus cur would be cur + 1 = 1
=> ans would be ans + cur = 0 + 1 = 1
For index 1:
=> arr[i] = cur – 1
=> Means it is a duplicate
=> Increase the count variable, count = 0 + 1 = 1
For index 2:
=> arr[i] = cur
=> cur would be cur + 1 = 2
=> ans would be ans + cur = 1 + 2 = 3
For index 3:
=> arr[i] = cur,
=> Thus cur would be cur + 1 = 3
=> ans would be ans + cur = 3 + 3 = 6
For index 4:
=> arr[i] > cur
=> cur would be the same for all the remaining subarrays.
=> Thus, count = count + 6 – 4 = 1 + 2(remaining subarrays) = 3 and break the loop.
In the end add cur * count to ans, thus ans = 6 + 3 * 3 = 15
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxSum(vector< int > arr)
{
int ans = 0;
sort(arr.begin(), arr.end());
int temp = 0, cou = 0;
for ( int i = 0; i < arr.size(); i++) {
if (arr[i] == temp) {
temp++;
ans += temp;
}
else if (temp - 1 == arr[i]) {
cou++;
}
else {
if (temp < arr[i]) {
cou += arr.size() - i;
break ;
}
}
}
if (cou != 0)
ans = ans + (cou * temp);
return ans;
}
int main()
{
int N = 3;
vector< int > arr = { 2, 1, 0 };
cout << maxSum(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maxSum( int [] arr)
{
int ans = 0 ;
Arrays.sort(arr);
int temp = 0 , cou = 0 ;
for ( int i = 0 ; i < arr.length; i++) {
if (arr[i] == temp) {
temp++;
ans += temp;
}
else if (temp - 1 == arr[i]) {
cou++;
}
else {
if (temp < arr[i]) {
cou += arr.length - i;
break ;
}
}
}
if (cou != 0 )
ans = ans + (cou * temp);
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
int [] arr = { 2 , 1 , 0 };
System.out.println(maxSum(arr));
}
}
|
Python3
def maxSum(arr) :
ans = 0
arr.sort()
temp = 0
cou = 0
for i in range ( 0 , len (arr)) :
if (arr[i] = = temp) :
temp + = 1
ans + = temp
elif (temp - 1 = = arr[i]) :
cou + = 1
else :
if (temp < arr[i]) :
cou + = len (arr) - i
break
if (cou ! = 0 ) :
ans = ans + (cou * temp)
return ans
if __name__ = = "__main__" :
N = 3
arr = [ 2 , 1 , 0 ]
print (maxSum(arr))
|
C#
using System;
class GFG
{
static int maxSum( int [] arr)
{
int ans = 0;
Array.Sort(arr);
int temp = 0, cou = 0;
for ( int i = 0; i < arr.Length; i++) {
if (arr[i] == temp) {
temp++;
ans += temp;
}
else if (temp - 1 == arr[i]) {
cou++;
}
else {
if (temp < arr[i]) {
cou += arr.Length - i;
break ;
}
}
}
if (cou != 0)
ans = ans + (cou * temp);
return ans;
}
public static void Main( string [] args)
{
int [] arr = { 2, 1, 0 };
Console.WriteLine(maxSum(arr));
}
}
|
Javascript
<script>
function maxSum(arr)
{
let ans = 0;
arr.sort( function (a, b) {
return a - b;
});
let temp = 0, cou = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] == temp) {
temp++;
ans += temp;
}
else if (temp - 1 == arr[i]) {
cou++;
}
else {
if (temp < arr[i]) {
cou += arr.length - i;
break ;
}
}
}
if (cou != 0)
ans = ans + (cou * temp);
return ans;
}
let N = 3;
let arr = [ 2, 1, 0 ];
document.write(maxSum(arr));
</script>
|
Time Complexity: O(N * log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...