给定一个大小为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)
高效的方法:为了优化上述方法,想法是使用Hashing。请注意,可以删除的最大元素数是大于MEX的元素数。请按照以下步骤解决问题:
- 初始化长度为N+1的数组hash[] ,如果给定数组中存在元素i,则hash[i]将为1 ,否则hash[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
Javascript
2
时间复杂度: O(N)
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。