📌  相关文章
📜  查找其按位异或等于K的N个不同的数字

📅  最后修改于: 2021-04-21 23:01:32             🧑  作者: Mango

给定两个正整数NX ,任务是构造N个正整数,所有这些整数的按位XOR等于K。

例子:

方法:解决此问题的想法是包括第一个(N – 3)个自然数并计算其按位XOR,然后根据计算出的XOR值选择其余3个整数。请按照以下步骤解决问题:

  • 如果N = 1:打印整数K本身。
  • 如果N = 2:打印K0作为所需的输出。
  • 否则,请考虑第一个(N – 3)个自然数。
  • 现在,计算(N – 3)个元素的按位XOR并将其存储在变量中,例如val 。可以根据以下条件选择其余三个元素:
    • 情况1:如果val等于K ,请执行以下步骤:
      • 在这种情况下,其余三个元素的按位XOR必须等于零,以使所有整数的按位XOR等于K。
      • 因此,其余三个元素必须为P,Q,P XOR Q ,从而使其按位XOR等于0
    • 情况2:如果val不等于K ,请执行以下步骤:
      • 在这种情况下,其余三个元素的按位XOR以及前(N – 3)个元素的按位XOR必须等于K。
      • 通过考虑最后三个元素等于0,P,P XOR K XOR val ,这三个元素的按位XOR等于K XOR val
  • 选择P和Q:对于这两种情况, PQ的值都必须与前(N – 3)个元素不同。因此,考虑PQ(N – 2)(N – 1)

以下是上述解决方案的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find N integers
// having Bitwise XOR equal to K
void findArray(int N, int K)
{
    // Base Cases
    if (N == 1) {
        printf("%d", K);
        return;
    }
 
    if (N == 2) {
        printf("%d %d", 0, K);
        return;
    }
 
    // Assign values to P and Q
    int P = N - 2;
    int Q = N - 1;
 
    // Stores Bitwise XOR of the
    // first (N - 3) elements
    int VAL = 0;
 
    // Print the first N - 3 elements
    for (int i = 1; i <= (N - 3); i++) {
 
        printf("%d ", i);
 
        // Calcualte Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
 
    if (VAL == K) {
        printf("%d %d %d", P, Q, P ^ Q);
    }
 
    else {
        printf("%d %d %d", 0,
               P, P ^ K ^ VAL);
    }
}
 
// Driver Code
int main()
{
    int N = 4, X = 6;
 
    // Function Call
    findArray(N, X);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
    
class GFG{
 
// Function to find N integers
// having Bitwise XOR equal to K
static void findArray(int N, int K)
{
     
    // Base Cases
    if (N == 1)
    {
        System.out.print(K + " ");
        return;
    }
   
    if (N == 2)
    {
        System.out.print(0 + " " + K);
        return;
    }
   
    // Assign values to P and Q
    int P = N - 2;
    int Q = N - 1;
   
    // Stores Bitwise XOR of the
    // first (N - 3) elements
    int VAL = 0;
   
    // Print the first N - 3 elements
    for(int i = 1; i <= (N - 3); i++)
    {
        System.out.print(i + " ");
         
        // Calcualte Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
   
    if (VAL == K)
    {
        System.out.print(P + " " +
                         Q + " " + (P ^ Q));
    }
    else
    {
        System.out.print(0 + " " +
                         P + " " +
                        (P ^ K ^ VAL));
    }
}
    
// Driver Code
public static void main(String[] args)
{
    int N = 4, X = 6;
     
    // Function Call
    findArray(N, X);
}
}
 
// This code is contributed by susmitakundugoaldanga


Python3
# Python3 program for the above approach
  
# Function to find N integers
# having Bitwise XOR equal to K
def findArray(N, K):
     
    # Base Cases
    if (N == 1):
        print(K, end = " ")
        return
     
    if (N == 2):
        print("0", end = " ")
        print(K, end = " ")
        return
     
    # Assign values to P and Q
    P = N - 2
    Q = N - 1
  
    # Stores Bitwise XOR of the
    # first (N - 3) elements
    VAL = 0
  
    # Print the first N - 3 elements
    for i in range(1, N - 2):
        print(i, end = " ")
         
        # Calcualte Bitwise XOR of
        # first (N - 3) elements
        VAL ^= i
         
    if (VAL == K):
        print(P, end = " ")
        print(Q, end = " ")
        print(P ^ Q, end = " ")
     
    else:
        print("0", end = " ")
        print(P , end = " ")
        print(P ^ K ^ VAL, end = " ")
 
# Driver Code
N = 4
X = 6
  
# Function Call
findArray(N, X)
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find N integers
// having Bitwise XOR equal to K
static void findArray(int N, int K)
{
    // Base Cases
    if (N == 1) {
        Console.Write(K + " ");
        return;
    }
  
    if (N == 2) {
        Console.Write(0 + " " + K);
        return;
    }
  
    // Assign values to P and Q
    int P = N - 2;
    int Q = N - 1;
  
    // Stores Bitwise XOR of the
    // first (N - 3) elements
    int VAL = 0;
  
    // Print the first N - 3 elements
    for (int i = 1; i <= (N - 3); i++) {
  
        Console.Write(i + " ");
  
        // Calcualte Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
  
    if (VAL == K) {
        Console.Write(P + " " + Q + " " + (P ^ Q));
    }
  
    else {
        Console.Write(0 + " " + P + " " + (P ^ K ^ VAL));
    }
}
    
// Driver Code
public static void Main()
{
    int N = 4, X = 6;
  
    // Function Call
    findArray(N, X);
}
}
 
// This code is contributed by code_hunt.


输出:

1 0 2 5

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