📜  生成的 N+1 个整数序列的 MEX,其中第 i 个整数是 (i-1) 和 K 的 XOR

📅  最后修改于: 2022-05-13 01:56:06.545000             🧑  作者: Mango

生成的 N+1 个整数序列的 MEX,其中第 i 个整数是 (i-1) 和 K 的 XOR

给定两个整数NK ,生成一个大小为N+1的序列,其中第i元素是(i-1)⊕K ,任务是找到这个序列的MEX 。这里,序列的MEX是序列中不出现的最小非负整数。

例子:

原生方法:解决这个问题的最简单方法是简单地制作给定数组并计算其MEX 。以下是解决此问题的步骤:

  1. 生成序列并对其进行排序。
  2. MEX初始化为 0 并遍历已排序的数组,如果当前元素等于MEX ,则将MEX增加 1,否则中断循环。
  3. 打印MEX作为此问题的答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the MEX of sequence
int findMex(int N, int K)
{
 
    // Generating the sequence
    int A[N + 1];
    for (int i = 0; i <= N; i++) {
        A[i] = i ^ K;
    }
 
    // Sorting the array
    sort(A, A + N + 1);
 
    // Calculating the MEX
    // Initialising the MEX variable
    int MEX = 0;
    for (int x : A) {
        if (x == MEX) {
 
            // If current MEX is equal
            // to the element
            // increase MEX by one
            MEX++;
        }
        else {
            // If current MEX is not equal
            // to the element then
            // the current MEX is the answer
            break;
        }
    }
    return MEX;
}
 
// Driver code
int main()
{
    // Given Input
    int N = 7, K = 3;
    cout << findMex(N, K);
    return 0;
}


Java
// Java program to find the Highest
// Power of M that divides N
import java.util.*;
public class GFG
{
 
  // Function to find the MEX of sequence
  static int findMex(int N, int K)
  {
 
    // Generating the sequence
    int[] A = new int[N + 1];
    for(int i = 0; i <= N; i++)
    {
      A[i] = i ^ K;
    }
 
    // Sorting the array
    Arrays.sort(A);
 
    // Calculating the MEX
    // Initialising the MEX variable
    int MEX = 0;
    for(int i = 0; i < A.length; i++)
    {
      if (A[i] == MEX)
      {
 
        // If current MEX is equal
        // to the element
        // increase MEX by one
        MEX++;
      }
      else
      {
 
        // If current MEX is not equal
        // to the element then
        // the current MEX is the answer
        break;
      }
    }
    return MEX;
  }
 
  // Driver code
  public static void main(String args[])
  {
    // Given Input
    int N = 7, K = 3;
    System.out.println(findMex(N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python program for the above approach
 
# Function to find the MEX of sequence
def findMex(N, K):
 
    # Generating the sequence
    A = [0] * (N + 1)
    for i in range(N + 1):
        A[i] = i ^ K
 
    # Sorting the array
    A.sort()
 
    # Calculating the MEX
    # Initialising the MEX variable
    MEX = 0
    for x in A:
        if (x == MEX):
 
            # If current MEX is equal
            # to the element
            # increase MEX by one
            MEX += 1
        else:
            # If current MEX is not equal
            # to the element then
            # the current MEX is the answer
            break
    return MEX
 
# Driver code
 
# Given Input
N = 7
K = 3
print(findMex(N, K))
 
# This code is contributed by Saurabh Jaiswal


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the MEX of sequence
static int findMex(int N, int K)
{
     
    // Generating the sequence
    int[] A = new int[N + 1];
    for(int i = 0; i <= N; i++)
    {
        A[i] = i ^ K;
    }
 
    // Sorting the array
    Array.Sort(A);
 
    // Calculating the MEX
    // Initialising the MEX variable
    int MEX = 0;
    foreach(int x in A)
    {
        if (x == MEX)
        {
             
            // If current MEX is equal
            // to the element
            // increase MEX by one
            MEX++;
        }
        else
        {
             
            // If current MEX is not equal
            // to the element then
            // the current MEX is the answer
            break;
        }
    }
    return MEX;
}
 
// Driver code
public static void Main()
{
     
    // Given Input
    int N = 7, K = 3;
     
    Console.WriteLine(findMex(N, K));
}
}
 
// This code is contributed by ukasp


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the MEX of the sequence
int findMEX(int N, int K)
{
 
    // Calculating the last possible bit
    int lastBit = max(round(log2(N + 1)),
                      round(log2(K)));
 
    // Initialising X
    int X = 0;
 
    for (int i = lastBit; i >= 0; i--) {
 
        // If the ith bit of K is equal
        // to the ith bit of N+1 then the
        // ith bit of X will remain 0
        if ((K >> i & 1) == ((N + 1)
                                 >> i
                             & 1))
            continue;
 
        // If the ith bit of K is equal to 0
        // and the ith bit of N+1 is equal to 1,
        // set the ith bit of X to 1
        else if ((K >> i & 1) == 0)
            X = X | (1 << i);
 
        // If the ith bit of K is equal to 1
        // and the ith bit of N+1 is equal to 0,
        // all the remaining bits will
        // remain unset
        else
            break;
    }
    return X;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 6, K = 4;
    cout << findMEX(N, K);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
public class GFG
{
     
// Function to calculate the
// log base 2 of an integer
static int log2(int N)
{
   
    // calculate log2 N indirectly
    // using log() method
    int result = (int)(Math.log(N) / Math.log(2));
   
    return result;
}
 
// Function to find the MEX of the sequence
static int findMEX(int N, int K)
{
 
    // Calculating the last possible bit
    int lastBit = Math.max(Math.round(log2(N + 1)),
                      Math.round(log2(K)));
 
    // Initialising X
    int X = 0;
 
    for (int i = lastBit; i >= 0; i--) {
 
        // If the ith bit of K is equal
        // to the ith bit of N+1 then the
        // ith bit of X will remain 0
        if ((K >> i & 1) == ((N + 1)
                                 >> i & 1))
            continue;
 
        // If the ith bit of K is equal to 0
        // and the ith bit of N+1 is equal to 1,
        // set the ith bit of X to 1
        else if ((K >> i & 1) == 0)
            X = X | (1 << i);
 
        // If the ith bit of K is equal to 1
        // and the ith bit of N+1 is equal to 0,
        // all the remaining bits will
        // remain unset
        else
            break;
    }
    return X;
}
 
// Driver Code
public static void main(String args[])
{
    // Given Input
    int N = 6, K = 4;
    System.out.println(findMEX(N, K));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.


Python3
# Python program for the above approach
from math import *
 
# Function to find the MEX of the sequence
def findMEX(N, K):
     
    # Calculating the last possible bit
    lastBit = max(round(log2(N + 1)), round(log2(K)))
     
    # Initialising X
    X = 0
 
    for i in range(lastBit, -1, -1):
         
        # If the ith bit of K is equal
        # to the ith bit of N+1 then the
        # ith bit of X will remain 0
        if ((K >> i & 1) == ((N + 1) >> i & 1)):
            continue
         
        # If the ith bit of K is equal to 0
        # and the ith bit of N+1 is equal to 1,
        # set the ith bit of X to 1
        elif ((K >> i & 1) == 0):
            X = X | (1 << i)
             
        # If the ith bit of K is equal to 1
        # and the ith bit of N+1 is equal to 0,
        # all the remaining bits will
        # remain unset
        else:
            break
     
    return X
 
# Driver Code
 
# Given Input
N = 6
K = 4
print(findMEX(N, K))
 
# This code is contributed by Shubham Singh


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to calculate the
  // log base 2 of an integer
  static double log2(int N)
  {
 
    // calculate log2 N indirectly
    // using log() method
    double result = (Math.Log(N) / Math.Log(2));
 
    return result;
  }
 
  // Function to find the MEX of the sequence
  static int findMEX(int N, int K)
  {
 
    // Calculating the last possible bit
    int lastBit = Math.Max((int)Math.Round(log2(N + 1)),
                           (int)Math.Round(log2(K)));
 
    // Initialising X
    int X = 0;
 
    for (int i = lastBit; i >= 0; i--) {
 
      // If the ith bit of K is equal
      // to the ith bit of N+1 then the
      // ith bit of X will remain 0
      if ((K >> i & 1) == ((N + 1)
                           >> i & 1))
        continue;
 
      // If the ith bit of K is equal to 0
      // and the ith bit of N+1 is equal to 1,
      // set the ith bit of X to 1
      else if ((K >> i & 1) == 0)
        X = X | (1 << i);
 
      // If the ith bit of K is equal to 1
      // and the ith bit of N+1 is equal to 0,
      // all the remaining bits will
      // remain unset
      else
        break;
    }
    return X;
  }
 
  // Driver Code
  public static void Main()
  {
    // Given Input
    int N = 6, K = 4;
    Console.Write(findMEX(N, K));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
8

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

有效方法:如果一个数M出现在这个序列中,那么对于第 i(i-1)⊕K = M ,这也意味着M⊕K = (i-1) ,其中 ( i-1的最大值)N 。现在,假设X是给定序列的 MEX,那么X⊕K必须大于N ,即X⊕K > N。所以, X的最小值是序列的 MEX。请按照以下步骤解决此问题:

  1. 从后面遍历N+1K的位。
  2. 如果K第 i位等于N+1的第i位,则将Xi位设置为 0。
  3. 如果K第 i位等于0 ,并且N+1i位等于 1,则将X第 i位设置为 1。
  4. 如果K第 i位等于1并且N+1i位等于0 ,则将X的所有剩余低位设置为 0,因为这意味着在该位置包含设置位的数字是缺失,它是序列的 MEX。
  5. 根据以上观察打印答案。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the MEX of the sequence
int findMEX(int N, int K)
{
 
    // Calculating the last possible bit
    int lastBit = max(round(log2(N + 1)),
                      round(log2(K)));
 
    // Initialising X
    int X = 0;
 
    for (int i = lastBit; i >= 0; i--) {
 
        // If the ith bit of K is equal
        // to the ith bit of N+1 then the
        // ith bit of X will remain 0
        if ((K >> i & 1) == ((N + 1)
                                 >> i
                             & 1))
            continue;
 
        // If the ith bit of K is equal to 0
        // and the ith bit of N+1 is equal to 1,
        // set the ith bit of X to 1
        else if ((K >> i & 1) == 0)
            X = X | (1 << i);
 
        // If the ith bit of K is equal to 1
        // and the ith bit of N+1 is equal to 0,
        // all the remaining bits will
        // remain unset
        else
            break;
    }
    return X;
}
 
// Driver Code
int main()
{
    // Given Input
    int N = 6, K = 4;
    cout << findMEX(N, K);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
public class GFG
{
     
// Function to calculate the
// log base 2 of an integer
static int log2(int N)
{
   
    // calculate log2 N indirectly
    // using log() method
    int result = (int)(Math.log(N) / Math.log(2));
   
    return result;
}
 
// Function to find the MEX of the sequence
static int findMEX(int N, int K)
{
 
    // Calculating the last possible bit
    int lastBit = Math.max(Math.round(log2(N + 1)),
                      Math.round(log2(K)));
 
    // Initialising X
    int X = 0;
 
    for (int i = lastBit; i >= 0; i--) {
 
        // If the ith bit of K is equal
        // to the ith bit of N+1 then the
        // ith bit of X will remain 0
        if ((K >> i & 1) == ((N + 1)
                                 >> i & 1))
            continue;
 
        // If the ith bit of K is equal to 0
        // and the ith bit of N+1 is equal to 1,
        // set the ith bit of X to 1
        else if ((K >> i & 1) == 0)
            X = X | (1 << i);
 
        // If the ith bit of K is equal to 1
        // and the ith bit of N+1 is equal to 0,
        // all the remaining bits will
        // remain unset
        else
            break;
    }
    return X;
}
 
// Driver Code
public static void main(String args[])
{
    // Given Input
    int N = 6, K = 4;
    System.out.println(findMEX(N, K));
 
}
}
 
// This code is contributed by Samim Hossain Mondal.

Python3

# Python program for the above approach
from math import *
 
# Function to find the MEX of the sequence
def findMEX(N, K):
     
    # Calculating the last possible bit
    lastBit = max(round(log2(N + 1)), round(log2(K)))
     
    # Initialising X
    X = 0
 
    for i in range(lastBit, -1, -1):
         
        # If the ith bit of K is equal
        # to the ith bit of N+1 then the
        # ith bit of X will remain 0
        if ((K >> i & 1) == ((N + 1) >> i & 1)):
            continue
         
        # If the ith bit of K is equal to 0
        # and the ith bit of N+1 is equal to 1,
        # set the ith bit of X to 1
        elif ((K >> i & 1) == 0):
            X = X | (1 << i)
             
        # If the ith bit of K is equal to 1
        # and the ith bit of N+1 is equal to 0,
        # all the remaining bits will
        # remain unset
        else:
            break
     
    return X
 
# Driver Code
 
# Given Input
N = 6
K = 4
print(findMEX(N, K))
 
# This code is contributed by Shubham Singh

C#

// C# program for the above approach
using System;
class GFG
{
 
  // Function to calculate the
  // log base 2 of an integer
  static double log2(int N)
  {
 
    // calculate log2 N indirectly
    // using log() method
    double result = (Math.Log(N) / Math.Log(2));
 
    return result;
  }
 
  // Function to find the MEX of the sequence
  static int findMEX(int N, int K)
  {
 
    // Calculating the last possible bit
    int lastBit = Math.Max((int)Math.Round(log2(N + 1)),
                           (int)Math.Round(log2(K)));
 
    // Initialising X
    int X = 0;
 
    for (int i = lastBit; i >= 0; i--) {
 
      // If the ith bit of K is equal
      // to the ith bit of N+1 then the
      // ith bit of X will remain 0
      if ((K >> i & 1) == ((N + 1)
                           >> i & 1))
        continue;
 
      // If the ith bit of K is equal to 0
      // and the ith bit of N+1 is equal to 1,
      // set the ith bit of X to 1
      else if ((K >> i & 1) == 0)
        X = X | (1 << i);
 
      // If the ith bit of K is equal to 1
      // and the ith bit of N+1 is equal to 0,
      // all the remaining bits will
      // remain unset
      else
        break;
    }
    return X;
  }
 
  // Driver Code
  public static void Main()
  {
    // Given Input
    int N = 6, K = 4;
    Console.Write(findMEX(N, K));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.

Javascript


输出
3

时间复杂度: O(log(max(N, K))
辅助空间: O(1)