Rearrange array elements to maximize the sum of MEX of all prefix arrays | Function to find the maximum sum of MEX of prefix arrays for any arrangement of the given array ; Stores the final arrangement ; Sort the array in increasing order ; Iterate over the array arr [ ] ; Iterate over the array , arr [ ] and push the remaining occurrences of the elements into ans [ ] ; Print the array , ans [ ] ; Given array ; Store the size of the array ; Function Call
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 ) ;