Open In App

Find N distinct numbers whose Bitwise XOR is equal to K

Given two positive integers N and K, the task is to construct N positive integers having Bitwise XOR of all these integers equal to K.

Examples:



Input: N = 4, K = 6
Output: 1 0 2 5
Explanation: Bitwise XOR the integers {1, 0, 2, 5} = 1 XOR 0 XOR 2 XOR 5 = 6(= K).

Input: N = 1, K = 1
Output: 1



Approach: The idea to solve this problem is to include the first (N – 3) natural numbers and calculate their Bitwise XOR and select the remaining 3 integers based on the calculated XOR values. Follow the steps below to solve the problem:

Below is the implementation of the above solution:




// C++ program for the above approach
#include <bits/stdc++.h>
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;
 
        // Calculate Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
 
    if (VAL == K)
    { // checking whether P ^ Q is greater than P or not.   
      while( (P ^ Q) <= P)
            {    Q++;
            }
        cout << " " << P << " " << Q
             << " " << (P ^ Q);
    }
 
    else
    { // checking whether P ^ K ^ VAL is greater than N-2 or not.
  
      while( (P ^ K ^ VAL) <= N-3)
        {    P++;
        }
        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 program for the above approach
#include <stdio.h>
 
// 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);
 
        // Calculate Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
 
    if (VAL == K) {
          // checking whether P ^ Q is greater than P or not.   
      while( (P ^ Q) <= P)
            {    Q++;
            }
        printf("%d %d %d", P, Q, P ^ Q);
    }
 
    else {
      // checking whether P ^ K ^ VAL is greater than N-2 or not.   
      while( (P ^ K ^ VAL) <= N-2)
        {    P++;
        }
        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 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 + " ");
         
        // Calculate Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
   
    if (VAL == K)
    {    // checking whether P ^ Q is greater than P or not.   
      while( (P ^ Q) <= P)
            {    Q++;
            }
        System.out.print(P + " " +
                         Q + " " + (P ^ Q));
    }
    else
    {    // checking whether P ^ K ^ VAL is greater than N-2 or not.   
      while( (P ^ K ^ VAL) <= N-2)
        {    P++;
        }
        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 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=" ")
 
        # Calculate Bitwise XOR of
        # first (N - 3) elements
        VAL ^= i
    if (VAL == K):
        # checking whether P ^ Q is greater than P or not.
        while ((P ^ Q) <= P):
            Q = Q + 1
        print(P, end=" ")
        print(Q, end=" ")
        print(P ^ Q, end=" ")
    else:
        # checking whether P ^ K ^ VAL is greater than N-2 or not.
        while ((P ^ K ^ VAL) <= N - 2):
            P = P + 1
        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# 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 + " ");
  
        // Calculate Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
  
    if (VAL == K) {
        // checking whether P ^ Q is greater than P or not.   
        while( (P ^ Q) <= P)
            {    Q++;
            }
        Console.Write(P + " " + Q + " " + (P ^ Q));
    }
  
    else {
        // checking whether P ^ K ^ VAL is greater than N-2 or not.   
        while( (P ^ K ^ VAL) <= N-2)
        {    P++;
        }
        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.




<script>
 
// Javascript program for the above approach
 
// Function to find N integers
// having Bitwise XOR equal to K
function findArray(N, K)
{
      
    // Base Cases
    if (N == 1)
    {
        document.write(K + " ");
        return;
    }
    
    if (N == 2)
    {
        document.write(0 + " " + K);
        return;
    }
    
    // Assign values to P and Q
    let P = N - 2;
    let Q = N - 1;
    
    // Stores Bitwise XOR of the
    // first (N - 3) elements
    let VAL = 0;
    
    // Print the first N - 3 elements
    for(let i = 1; i <= (N - 3); i++)
    {
        document.write(i + " ");
          
        // Calculate Bitwise XOR of
        // first (N - 3) elements
        VAL ^= i;
    }
    
    if (VAL == K)
    {    // checking whether P ^ Q is greater than P or not.   
        while( (P ^ Q) <= P)
            {    Q++;
            }
        document.write(P + " " +
                         Q + " " + (P ^ Q));
    }
    else
    {    // checking whether P ^ K ^ VAL is greater than N-2 or not.   
        while( (P ^ K ^ VAL) <= N-2)
        {    P++;
        }
        document.write(0 + " " +
                         P + " " +
                        (P ^ K ^ VAL));
    }
}
 
// Driver Code
 
      let N = 4, X = 6;
      
    // Function Call
    findArray(N, X);
  
</script>

Output:

1 0 2 5

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :