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

📅  最后修改于: 2021-05-17 20:58:59             🧑  作者: Mango

给定具有N个不同的正元素的数组arr [] ,任务是生成另一个数组B [] ,以便对于数组arr []中的i索引, 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)

高效方法:为了优化上述方法,这个想法是计算阵列ARRMEX []和遍历数组ARR [],如果ARR [i]是小于阵列ARRMEX []然后MEX不含此元件将是ARR [I]自身,如果ARR [I]比阵列的MEX大于A []然后阵列的MEX将不排除该元素之后发生变化。

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

  1. 初始化一个数组,例如hash [] ,以存储天气值i是否存在于数组arr []中(如果存在i ) hash [i] = 1,否则hash [i] = 0。
  2. 初始化变量MexOfArr来存储数组arr []的MEX ,并从1遍历数组hash []来找到最小索引j ,其哈希值[j] = 0 ,这意味着值j不存在于数组arr []中,并且存储MexOfArr = j
  3. 现在遍历数组arr [] ,如果arr [i]小于MexOfArr,则存储B [i] = arr [i],否则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


输出:
2 1 4 3









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