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

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

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

例子:

方法:解决这个问题的想法是包括前(N-3)个自然数并计算它们的按位异或,并根据计算出的异或值选择剩余的 3 个整数。请按照以下步骤解决问题:

  • 如果 N = 1:打印整数K本身。
  • 如果 N = 2:打印K0作为所需的输出。
  • 否则,考虑前(N – 3) 个自然数。
  • 现在,计算(N – 3) 个元素的按位异或并将其存储在一个变量中,比如val 。其余三个元素可以根据以下条件进行选择:
    • 情况 1:如果val等于K ,则执行以下步骤:
      • 在这种情况下,其余三个元素的 Bitwise XOR 需要等于 0 才能使所有整数的 Bitwise XOR 等于K
      • 因此,其余三个元素必须是P, Q, P XOR Q ,从而使它们的 Bitwise XOR 等于0
    • 情况 2:如果val不等于K ,则执行以下步骤:
      • 在这种情况下,其余三个元素的按位异或加上前(N – 3) 个元素的按位异或需要等于K
      • 通过考虑最后 3 个元素等于0、P、P XOR K XOR val ,这三个元素的按位异或等于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)
    {
        cout << " " << K;
        return;
    }
 
    if (N == 2)
    {
        cout << 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++)
    {
        cout << " " << i;
 
        // Calcualte Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
 
    if (VAL == K)
    {
        cout << P << " " << Q
             << " " << (P ^ Q);
    }
 
    else
    {
        cout << 0 << " " << P
             << " " << (P ^ K ^ VAL);
    }
}
 
// Driver Code
int main()
{
    int N = 4, X = 6;
 
    // Function Call
    findArray(N, X);
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110


C
// C program for the above approach
#include 
 
// 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.


Javascript


输出:

1 0 2 5

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