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++
// 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; } |
Java
// 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 |
Python3
# Python3 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 |
C#
// 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 |
Javascript
<script> // Javascript program to implement // the above approach // Function to find the maximum number // of elements that can be removed function countRemovableElem(arr, N) { // Initialize hash[] with 0s let hash = []; for (let i = 0; i < N; i++) { hash[i] = 0; } // Initialize MEX let mex = N + 1; // Set hash[i] = 1, if i is // present in arr[] for (let i = 0; i < N; i++) { if (arr[i] <= N) hash[arr[i]] = 1; } // Find MEX from the hash for (let i = 1; i <= N; i++) { if (hash[i] == 0) { mex = i; break ; } } // Print the maximum numbers // that can be removed document.write(N - (mex - 1)); } // Driver Code // Given array let arr = [2, 3, 5, 1, 6 ]; // Size of the array let N = arr.length; // Function Call countRemovableElem(arr, N); </script> |
2
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...