Maximum number of elements that can be removed such that MEX of the given array remains unchanged
Given an array arr[] of size N, the task is to count the maximum number of elements that can be removed from the given array without changing the MEX of the original array.
The MEX is the smallest positive integer that is not present in the array.
Examples:
Input: arr[] = {2, 3, 5, 1, 6}
Output: 2
Explanation:
The smallest positive integer which is not present in the array is 4.
Hence, MEX of the given array is 4.
Therefore, 5 and 6 can be removed without changing the MEX of the array.
Input: arr[] = {12, 4, 6, 1, 7, 2}
Output: 4
Explanation:
The smallest positive integer which is not present in the array is 3.
Hence, MEX of the given array is 3.
Therefore, 4, 6, 7 and 12 can be removed without changing the MEX of the array.
Naive Approach: The simplest approach is to sort the array and then traverse the array from i = 0 while arr[i] equals to i + 1. After that, print the answer as (N – i) which is the maximum number of elements that can be removed from the given array without changing its MEX.
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Efficient Approach: To optimize the above approach, the idea is to use Hashing. Observe that, the maximum number of elements that can be removed is the number of elements that are greater than the MEX. Follow the steps below to solve the problem:
- Initialize an array hash[] of length N+1 where hash[i] will be 1 if element i is present in the given array otherwise hash[i] = 0.
- Initialize a variable mex with N + 1 to store MEX of the given array.
- Traverse array hash[] over the range [1, N], and if for any index hash[i] equals to 0, update mex as mex = i and break out of the loop.
- Print N – (mex – 1) as the maximum number of elements that can be removed from the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countRemovableElem(
int arr[], int N)
{
int hash[N + 1] = { 0 };
int mex = N + 1;
for ( int i = 0; i < N; i++) {
if (arr[i] <= N)
hash[arr[i]] = 1;
}
for ( int i = 1; i <= N; i++) {
if (hash[i] == 0) {
mex = i;
break ;
}
}
cout << N - (mex - 1);
}
int main()
{
int arr[] = { 2, 3, 5, 1, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
countRemovableElem(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void countRemovableElem( int [] arr, int N)
{
int [] hash = new int [N + 1 ];
Arrays.fill(hash, 0 );
int mex = N + 1 ;
for ( int i = 0 ; i < N; i++)
{
if (arr[i] <= N)
hash[arr[i]] = 1 ;
}
for ( int i = 1 ; i <= N; i++)
{
if (hash[i] == 0 )
{
mex = i;
break ;
}
}
System.out.println(N - (mex - 1 ));
}
public static void main(String[] args)
{
int [] arr = { 2 , 3 , 5 , 1 , 6 };
int N = arr.length;
countRemovableElem(arr, N);
}
}
|
Python3
def countRemovableElem(arr, N):
hash = [ 0 ] * (N + 1 )
mex = N + 1
for i in range ( 0 , N):
if (arr[i] < = N):
hash [arr[i]] = 1
for i in range ( 1 , N + 1 ):
if ( hash [i] = = 0 ):
mex = i
break
print (N - (mex - 1 ))
if __name__ = = '__main__' :
arr = [ 2 , 3 , 5 , 1 , 6 ]
N = len (arr)
countRemovableElem(arr, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void countRemovableElem( int [] arr, int N)
{
int [] hash = new int [N + 1];
Array.Fill(hash, 0);
int mex = N + 1;
for ( int i = 0; i < N; i++)
{
if (arr[i] <= N)
hash[arr[i]] = 1;
}
for ( int i = 1; i <= N; i++)
{
if (hash[i] == 0)
{
mex = i;
break ;
}
}
Console.WriteLine(N - (mex - 1));
}
public static void Main()
{
int [] arr = { 2, 3, 5, 1, 6 };
int N = arr.Length;
countRemovableElem(arr, N);
}
}
|
Javascript
<script>
function countRemovableElem(arr, N)
{
let hash = [];
for (let i = 0; i < N; i++)
{
hash[i] = 0;
}
let mex = N + 1;
for (let i = 0; i < N; i++)
{
if (arr[i] <= N)
hash[arr[i]] = 1;
}
for (let i = 1; i <= N; i++)
{
if (hash[i] == 0)
{
mex = i;
break ;
}
}
document.write(N - (mex - 1));
}
let arr = [2, 3, 5, 1, 6 ];
let N = arr.length;
countRemovableElem(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
24 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...