📌  相关文章
📜  可以删除的最大元素数,以便给定数组的 MEX 保持不变

📅  最后修改于: 2021-09-07 04:48:24             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是计算在不改变原始数组的MEX的情况下可以从给定数组中删除的最大元素数。

例子:

朴素的方法:最简单的方法是对数组进行排序,然后从i = 0开始遍历数组,而arr[i]等于i + 1 。之后,将答案打印为(N – i) ,这是可以从给定数组中删除而不改变其MEX的最大元素数。

时间复杂度: O(N*log N)
辅助空间: O(N)

高效的方法:为了优化上述方法,想法是使用Hashing。请注意,可以删除的最大元素数是大于MEX的元素数。请按照以下步骤解决问题:

  1. 初始化长度为N+1的数组hash[] ,如果给定数组中存在元素i,hash[i]将为1 ,否则hash[i] = 0。
  2. N + 1初始化一个变量mex来存储给定数组的MEX
  3. 在范围[1, N] 上遍历数组hash[] ,如果任何索引hash[i]等于0 ,则将mex更新为mex = i并跳出循环。
  4. 打印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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live