Partition the given array in two parts to make the MEX of both parts same
Given an array arr[] containing N integers where 0 ≤ A[ i ] ≤ N, the task is to divide the array into two equal parts such that the MEX of both the parts are the same, otherwise print -1.
Note: The MEX (minimum excluded) of an array is the smallest non-negative integer that does not exist in the array.
Examples:
Input: N = 6, arr[] = {1, 6, 2, 3, 5, 2}
Output:
0
1 2 5
2 3 6
Explanation: After dividing the given array into {1, 2, 5} and {2, 3, 6}, MEX of both array is equal( i. e MEX = 0)
Input: N = 4, arr[] = {0, 0, 1, 2}
Output: -1
Explanation: Not possible to divide an array into two different array
Intuition: As mentioned, MEX is the smallest non-negative integer that does not belong to the array. So any non-negative smallest number which is not present in the given array can become the MEX for both output arrays.
To make an integer X, MEX for both arrays, all elements less than X must present at least once in both the output array.
For Example:
Array = [1, 4, 2, 4, 2, 1, 1]
Then divide the array into [1, 1, 2, 4] and [1, 2, 4]
If any element less than X is present in the given array only once, then it is not possible to divide the given array into two arrays of equal MEX.
Example:
Array = [1, 2, 3, 4, 4, 2, 1]
In this example, 3 is only present 1 time in given array so if we try to divide it into 2 array, then MEX of both array is not equal.
[1, 2, 3, 4] : MEX = 5
[1, 2, 4] : MEX = 3
Approach: The solution to the problem is based on the concept of hashmap. Follow the steps mentioned below:
- Create a hash map to check whether any unique element is present in the array or not.
- Iterate from 0 to N and in each iteration check
- If the frequency of that element is 1, then it is not possible to divide the array into two such that their MEX should be equal
- If the frequency of that element is 0, then we can divide the array.
- If the frequency of that element is more than 1, then check for the next element.
- Sort the given array and divide the array according to its even or odd index, so that if any element is present 2 or more than 2 times in the array then after dividing both output array consists at least 1 time that elements to maintain equal MEX.
- Print both the arrays.
Below is the code implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void MEX_arr( int * A, int N)
{
unordered_map< int , int > mp;
bool status = true ;
for ( int i = 0; i < N; i++) {
mp[A[i]]++;
}
for ( int i = 0; i <= N; i++) {
if (mp[i] == 0) {
cout << i << "\n" ;
break ;
}
if (mp[i] == 1) {
status = false ;
break ;
}
}
if (status == true ) {
sort(A, A + N);
int A1[N / 2], A2[N / 2];
for ( int i = 0; i < N / 2; i++) {
A1[i] = A[2 * i];
A2[i] = A[2 * i + 1];
}
for ( int i = 0; i < N / 2; i++) {
cout << A1[i] << " " ;
}
cout << "\n" ;
for ( int i = 0; i < N / 2; i++) {
cout << A2[i] << " " ;
}
}
else
cout << -1;
}
int main()
{
int N = 6;
int arr[N] = { 1, 6, 2, 3, 5, 2 };
unordered_map< int , int > mp;
MEX_arr(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static void MEX_arr( int A[], int N)
{
HashMap<Integer, Integer> mp = new HashMap<>();
boolean status = true ;
for ( int i = 0 ; i < N; i++) {
if (mp.get(A[i]) != null )
mp.put(A[i], mp.get(A[i]) + 1 );
else
mp.put(A[i], 1 );
}
for ( int i = 0 ; i <= N; i++) {
if (mp.get(i) == null ) {
System.out.println(i);
break ;
}
if (mp.get(i) == 1 ) {
status = false ;
break ;
}
}
if (status == true ) {
Arrays.sort(A);
int A1[] = new int [N / 2 ];
int A2[] = new int [N / 2 ];
for ( int i = 0 ; i < N / 2 ; i++) {
A1[i] = A[ 2 * i];
A2[i] = A[ 2 * i + 1 ];
}
for ( int i = 0 ; i < N / 2 ; i++) {
System.out.print(A1[i] + " " );
}
System.out.println();
for ( int i = 0 ; i < N / 2 ; i++) {
System.out.print(A2[i] + " " );
}
}
else
System.out.print(- 1 );
}
public static void main(String[] args)
{
int N = 6 ;
int arr[] = { 1 , 6 , 2 , 3 , 5 , 2 };
MEX_arr(arr, N);
}
}
|
Python3
def MEX_arr(A, N):
mp = {}
status = True
for i in range ( 0 , N):
mp[A[i]] = mp[A[i]] + 1 if A[i] in mp else 1
for i in range ( 0 , N + 1 ):
if ( not i in mp):
print (i)
break
elif (mp[i] = = 1 ):
status = False
break
if (status = = True ):
A.sort()
A1, A2 = [ 0 for _ in range (N / / 2 )], [ 0 for _ in range (N / / 2 )]
for i in range ( 0 , N / / 2 ):
A1[i] = A[ 2 * i]
A2[i] = A[ 2 * i + 1 ]
for i in range ( 0 , N / / 2 ):
print (A1[i], end = " " )
print ()
for i in range ( 0 , N / / 2 ):
print (A2[i], end = " " )
else :
print ( - 1 )
if __name__ = = "__main__" :
N = 6
arr = [ 1 , 6 , 2 , 3 , 5 , 2 ]
MEX_arr(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void MEX_arr( int []A, int N)
{
Dictionary< int , int > mp =
new Dictionary< int , int >();
bool status = true ;
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(A[i])) {
mp[A[i]] = mp[A[i]] + 1;
}
else {
mp.Add(A[i], 1);
}
}
for ( int i = 0; i <= N; i++) {
if (!mp.ContainsKey(i)) {
Console.WriteLine(i);
break ;
}
if (mp[i] == 1) {
status = false ;
break ;
}
}
if (status == true ) {
Array.Sort(A);
int []A1 = new int [N / 2];
int []A2 = new int [N / 2];
for ( int i = 0; i < N / 2; i++) {
A1[i] = A[2 * i];
A2[i] = A[2 * i + 1];
}
for ( int i = 0; i < N / 2; i++) {
Console.Write(A1[i] + " " );
}
Console.WriteLine();
for ( int i = 0; i < N / 2; i++) {
Console.Write(A2[i] + " " );
}
}
else
Console.Write(-1);
}
public static void Main()
{
int N = 6;
int []arr = { 1, 6, 2, 3, 5, 2 };
MEX_arr(arr, N);
}
}
|
Javascript
<script>
function MEX_arr(A, N) {
let mp = new Map();
var status = true ;
for (let i = 0; i < N; i++) {
if (!mp.has(A[i])) {
mp.set(A[i], 1);
}
else {
mp.set(A[i], mp.get(A[i]) + 1)
}
}
for (let i = 0; i <= N; i++) {
if (!mp.has(i)) {
document.write(i + '<br>' )
break ;
}
else if (mp.get(i) == 1) {
status = false ;
break ;
}
}
if (status == true ) {
A.sort( function (a, b) { return a - b })
let A1 = new Array(Math.floor(N / 2));
let A2 = new Array(Math.floor(N / 2));
for (let i = 0; i < Math.floor(N / 2); i++) {
A1[i] = A[2 * i];
A2[i] = A[2 * i + 1];
}
for (let i = 0; i < N / 2; i++) {
document.write(A1[i] + ' ' )
}
document.write( '<br>' )
for (let i = 0; i < N / 2; i++) {
document.write(A2[i] + ' ' )
}
}
else
document.write(-1)
}
let N = 6;
let arr = [1, 6, 2, 3, 5, 2];
MEX_arr(arr, N);
</script>
|
Time Complexity: O(N * log(N))
Auxiliary space: O(N)
Last Updated :
05 Apr, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...