Related Articles
Find N distinct numbers whose Bitwise XOR is equal to K
• Last Updated : 27 Apr, 2021

Given two positive integers N and X, 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:

• If N = 1: Print the integer K itself.
• If N = 2: Print K and 0 as the required output.
• Otherwise, consider the first (N – 3) natural numbers.
• Now, calculate Bitwise XOR of (N – 3) elements and store it in a variable, say val. The remaining three elements can be selected based on the following conditions:
• Case 1: If val is equal to K, perform the following steps:
• In this case, the Bitwise XOR of the remaining three elements needs to be equal to zero to make Bitwise XOR of all integers equal to K.
• Therefore, the remaining three elements must be P, Q, P XOR Q, thus making their Bitwise XOR equal to 0.
• Case 2: If val is not equal to K, perform the following steps:
• In this case, Bitwise XOR of the remaining three elements together with Bitwise XOR of the first (N – 3) elements needs to be equal to K.
• By considering the last 3 elements to be equal to 0, P, P XOR K XOR val, Bitwise XOR of these three elements is equal to K XOR val.
• Choosing P and Q: For both cases, the value of P and Q requires to be distinct from the first (N – 3) elements. Therefore, consider P and Q to be (N – 2) and (N – 1).

Below is the implementation of the above solution:

## 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.`

## Javascript

 ``

Output:

`1 0 2 5`

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

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up