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:
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:

  1. 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.
  2. Initialize a variable mex with N + 1 to store MEX of the given array.
  3. 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.
  4. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the maximum number
// of elements that can be removed
void countRemovableElem(
    int arr[], int N)
{
    // Initialize hash[] with 0s
    int hash[N + 1] = { 0 };
  
    // Initialize MEX
    int mex = N + 1;
  
    // Set hash[i] = 1, if i is
    // present in arr[]
    for (int i = 0; i < N; i++) {
        if (arr[i] <= N)
            hash[arr[i]] = 1;
    }
  
    // Find MEX from the hash
    for (int i = 1; i <= N; i++) {
        if (hash[i] == 0) {
            mex = i;
            break;
        }
    }
  
    // Print the maximum numbers
    // that can be removed
    cout << N - (mex - 1);
}
  
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 3, 5, 1, 6 };
  
    // Size of the array
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    countRemovableElem(arr, N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.io.*;
import java.util.*;
  
class GFG{
  
// Function to find the maximum number
// of elements that can be removed
static void countRemovableElem(int[] arr, int N)
{
      
    // Initialize hash[] with 0s
    int[] hash = new int[N + 1];
    Arrays.fill(hash, 0);
  
    // Initialize MEX
    int mex = N + 1;
  
    // Set hash[i] = 1, if i is
    // present in arr[]
    for(int i = 0; i < N; i++) 
    {
        if (arr[i] <= N)
            hash[arr[i]] = 1;
    }
  
    // Find MEX from the hash
    for(int i = 1; i <= N; i++) 
    {
        if (hash[i] == 0)
        {
            mex = i;
            break;
        }
    }
  
    // Print the maximum numbers
    // that can be removed
    System.out.println(N - (mex - 1));
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given array
    int[] arr = { 2, 3, 5, 1, 6 };
  
    // Size of the array
    int N = arr.length;
  
    // Function Call
    countRemovableElem(arr, N);
}
}
  
// This code is contributed by akhilsaini

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Pyhton3 program for the above approach
  
# Function to find the maximum number
# of elements that can be removed
def countRemovableElem(arr, N):
  
    # Initialize hash[] with 0s
    hash = [0] * (N + 1)
  
    # Initialize MEX
    mex = N + 1
  
    # Set hash[i] = 1, if i is
    # present in arr[]
    for i in range(0, N):
        if (arr[i] <= N):
            hash[arr[i]] = 1
  
    # Find MEX from the hash
    for i in range(1, N + 1):
        if (hash[i] == 0):
            mex = i
            break
  
    # Print the maximum numbers
    # that can be removed
    print(N - (mex - 1))
  
# Driver Code
if __name__ == '__main__':
      
    # Given array
    arr = [ 2, 3, 5, 1, 6 ]
  
    # Size of the array
    N = len(arr)
  
    # Function Call
    countRemovableElem(arr, N)
  
# This code is contributed by akhilsaini

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
using System.Collections.Generic;
  
class GFG{
  
// Function to find the maximum number
// of elements that can be removed
static void countRemovableElem(int[] arr, int N)
{
      
    // Initialize hash[] with 0s
    int[] hash = new int[N + 1];
    Array.Fill(hash, 0);
  
    // Initialize MEX
    int mex = N + 1;
  
    // Set hash[i] = 1, if i is
    // present in arr[]
    for(int i = 0; i < N; i++)
    {
        if (arr[i] <= N)
            hash[arr[i]] = 1;
    }
  
    // Find MEX from the hash
    for(int i = 1; i <= N; i++) 
    {
        if (hash[i] == 0)
        {
            mex = i;
            break;
        }
    }
  
    // Print the maximum numbers
    // that can be removed
    Console.WriteLine(N - (mex - 1));
}
  
// Driver Code
public static void Main()
{
      
    // Given array
    int[] arr = { 2, 3, 5, 1, 6 };
  
    // Size of the array
    int N = arr.Length;
  
    // Function Call
    countRemovableElem(arr, N);
}
}
  
// This code is contributed by akhilsaini

chevron_right


Output: 

2



 

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : akhilsaini

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.