给定大小为N的数组arr [] ,任务是计算可以在不更改原始数组MEX的情况下从给定数组中删除的最大元素数。
The MEX is the smallest positive integer that is not present in the array.
例子:
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.
天真的方法:最简单的方法是对数组进行排序,然后从i = 0遍历该数组,而arr [i]等于i + 1 。之后,将答案打印为(N – i) ,这是在不更改其MEX的情况下可以从给定数组中删除的最大元素数。
时间复杂度: O(N * log N)
辅助空间: O(N)
高效方法:为了优化上述方法,我们的想法是使用哈希。请注意,可以删除的最大元素数是大于MEX的元素数。请按照以下步骤解决问题:
- 初始化一个数组散列[长度] N + 1,其中散列[I]为1,如果元件i是存在于给定的阵列中,否则散列[I] = 0。
- 用N +1初始化变量mex以存储给定数组的MEX 。
- 在[1,N]范围内遍历数组hash [] ,如果任何索引hash [i]等于0 ,则将mex更新为mex = i并退出循环。
- 打印N –(mex – 1)作为可以从给定数组中删除的最大元素数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
时间复杂度: O(N)
辅助空间: O(N)