📌  相关文章
📜  从给定数组构造 MEX 数组

📅  最后修改于: 2021-10-26 05:27:45             🧑  作者: Mango

给定一个数组arr[]具有N 个不同的正元素,任务是生成另一个数组B[]使得对于数组中的每个i索引, arr[]B[i]是从arr 中丢失的最小正数[]不包括arr[i]

例子:

朴素的方法:解决这个问题最简单的方法是遍历数组arr[]并为每个索引i初始化一个数组hash[]并为每个索引j (其中ji )更新hash[arr[j]] =1 。现在从索引1遍历数组hash[]并找到hash[k] = 0的最小索引k并更新B[i] = k 。最后,完成上述步骤后打印数组B[]

时间复杂度: O(N 2 ) 其中 N 是给定数组的长度。
辅助空间: O(N)

高效方法:为了优化上述方法,思想是计算数组arr[] 的MEX并遍历数组arr[] 。如果ARR [i]是小于阵列ARRMEX []然后MEX不含此元件将是改编[I]本身,如果ARR [i]为比阵列AMEX []大于所述阵列的MEX不会排除此元素后更改。

请按照以下步骤解决问题:

  1. 初始化一个数组,比如hash[] ,以存储值i是否存在于数组arr[] 中。如果i存在hash[i] = 1否则hash[i] = 0。
  2. 初始化变量MexOfArr以存储数组arr[] 的MEX并从1遍历数组hash[]以找到hash[j] = 0的最小索引j 这意味着数组arr[] 中不存在值j并存储MexOfArr = j
  3. 现在遍历数组arr[]并且如果arr[i]小于MexOfArr,则存储B[i] = arr[i] else B[i] = MexOfArr
  4. 完成上述步骤后,将数组B[] 的元素打印为所需答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
#define MAXN 100001
 
// Function to construct array B[] that
// stores MEX of array A[] excluding A[i]
void constructMEX(int arr[], int N)
{
    // Stores elements present in arr[]
    int hash[MAXN] = { 0 };
 
    // Mark all values 1, if present
    for (int i = 0; i < N; i++) {
 
        hash[arr[i]] = 1;
    }
 
    // Initialize variable to store MEX
    int MexOfArr;
 
    // Find MEX of arr[]
    for (int i = 1; i < MAXN; i++) {
        if (hash[i] == 0) {
            MexOfArr = i;
            break;
        }
    }
 
    // Stores MEX for all indices
    int B[N];
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // Update MEX
        if (arr[i] < MexOfArr)
            B[i] = arr[i];
 
        // MEX default
        else
            B[i] = MexOfArr;
    }
 
    // Print the array B
    for (int i = 0; i < N; i++)
        cout << B[i] << ' ';
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 2, 1, 5, 3 };
 
    // Given size
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function call
    constructMEX(arr, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
     
static int MAXN = 100001;
   
// Function to construct array
// B[] that stores MEX of array
// A[] excluding A[i]
static void constructMEX(int arr[],
                         int N)
{
  // Stores elements present
  // in arr[]
  int hash[] = new int[MAXN];
  for (int i = 0; i < N; i++)
  {
    hash[i] = 0;
  }
 
  // Mark all values 1, if
  // present
  for (int i = 0; i < N; i++)
  {
    hash[arr[i]] = 1;
  }
 
  // Initialize variable to
  // store MEX
  int MexOfArr = 0 ;
 
  // Find MEX of arr[]
  for (int i = 1; i < MAXN; i++)
  {
    if (hash[i] == 0)
    {
      MexOfArr = i;
      break;
    }
  }
 
  // Stores MEX for all
  // indices
  int B[] = new int[N];
 
  // Traverse the given array
  for (int i = 0; i < N; i++)
  {
    // Update MEX
    if (arr[i] < MexOfArr)
      B[i] = arr[i];
 
    // MEX default
    else
      B[i] = MexOfArr;
  }
 
  // Print the array B
  for (int i = 0; i < N; i++)
    System.out.print(B[i] + " ");
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int arr[] = {2, 1, 5, 3};
 
  // Size of array
  int N = arr.length;
 
  // Function call
  constructMEX(arr, N);
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program for the
# above approach
 
MAXN = 100001
 
# Function to construct
# array B[] that stores
# MEX of array A[] excluding
# A[i]
def constructMEX(arr, N):
   
    # Stores elements present
    # in arr[]
    hash = [0] * MAXN
 
    # Mark all values 1,
    # if present
    for i in range(N):
        hash[arr[i]] = 1
 
    # Initialize variable to
    # store MEX
    MexOfArr = 0
 
    # Find MEX of arr[]
    for i in range(1, MAXN):
        if (hash[i] == 0):
            MexOfArr = i
            break
 
    # Stores MEX for all
    # indices
    B = [0] * N
 
    # Traverse the given array
    for i in range(N):
 
        # Update MEX
        if (arr[i] < MexOfArr):
            B[i] = arr[i]
 
        # MEX default
        else:
            B[i] = MexOfArr
 
    # Prthe array B
    for i in range(N):
        print(B[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [2, 1, 5, 3]
 
    # Given size
    N = len(arr)
 
    # Function call
    constructMEX(arr, N)
 
# This code is contributed by Mohit Kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
static int MAXN = 100001;
   
// Function to construct array
// B[] that stores MEX of array
// A[] excluding A[i]
static void constructMEX(int[] arr,
                         int N)
{
   
  // Stores elements present
  // in arr[]
  int[] hash = new int[MAXN];
  for(int i = 0; i < N; i++)
  {
    hash[i] = 0;
  }
 
  // Mark all values 1, if
  // present
  for(int i = 0; i < N; i++)
  {
    hash[arr[i]] = 1;
  }
 
  // Initialize variable to
  // store MEX
  int MexOfArr = 0;
 
  // Find MEX of arr[]
  for(int i = 1; i < MAXN; i++)
  {
    if (hash[i] == 0)
    {
      MexOfArr = i;
      break;
    }
  }
   
  // Stores MEX for all
  // indices
  int[] B = new int[N];
 
  // Traverse the given array
  for(int i = 0; i < N; i++)
  {
     
    // Update MEX
    if (arr[i] < MexOfArr)
      B[i] = arr[i];
 
    // MEX default
    else
      B[i] = MexOfArr;
  }
 
  // Print the array B
  for(int i = 0; i < N; i++)
    Console.Write(B[i] + " ");
}
 
// Driver Code
public static void Main()
{
   
  // Given array arr[]
  int[] arr = { 2, 1, 5, 3 };
   
  // Size of array
  int N = arr.Length;
   
  // Function call
  constructMEX(arr, N);
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
2 1 4 3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程