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
# 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 |
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 |
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.
Recommended Posts:
- Minimize K whose XOR with given array elements leaves array unchanged
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Maximize the first element of the array such that average remains constant
- Minimum number of elements to be removed such that the sum of the remaining elements is equal to k
- Check if all array elements can be removed by the given operations
- Maximum MEX from all subarrays of length K
- Minimum elements to be removed such that sum of adjacent elements is always odd
- Minimum elements to be removed such that sum of adjacent elements is always even
- Minimum operations to make the MEX of the given set equal to x
- Value of k-th index of a series formed by append and insert MEX in middle
- Maximize sum of array elements removed by performing the given operations
- Find the minimum number of elements that should be removed to make an array good
- Length of smallest subarray to be removed such that the remaining array is sorted
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Length of Longest sub-string that can be removed
- Minimum elements to be removed from the ends to make the array sorted
- Shortest subarray to be removed to make all Array elements unique
- Minimize count of array elements to be removed to maximize difference between any pair up to K
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Mimimum number of leaves required to be removed from a Tree to satisfy the given condition
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

