Find N distinct numbers whose Bitwise XOR is equal to K
Last Updated :
12 Sep, 2022
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:
- 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. But P XOR Q must be greater than N-2(i.e. P) so that all the numbers should remain distinct. ( In case, if you are wondering P XOR Q should also not be equal to Q, well this is not possible since the value of P will never be zero.
- Case (i): P XOR Q is already greater than P :
- The remaining three elements will be P, Q, P XOR Q.
- Case (ii): P XOR Q is not greater than P :
- Here, we will use the while loop with condition P XOR Q <= P with increment on Q by 1. After the termination of the loop, the remaining three elements will be P, Q, P XOR Q.
- 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. Again, P XOR K XOR val must be greater than N-2 for all the N numbers to be distinct.
- Case (i): P XOR K XOR val is already greater than N-2 :
- The remaining three elements will be 0, P, P XOR K XOR val.
- Case (ii): P XOR K XOR val is not greater than N-2:
- Here, we will use the while loop with condition P XOR K XOR val <= N-2 with increment on P by 1. After the termination of loop, the remaining three elements will be 0, P, P XOR 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).
- In case you are wondering that while loops may be stuck for too long, is not true. You will generally get the number in the range of 100 iterations. So on average, the time complexity of both the while loops mentioned in cases 1 and 2 is almost constant.
Below is the implementation of the above solution:
C++
#include <bits/stdc++.h>
using namespace std;
void findArray( int N, int K)
{
if (N == 1)
{
cout << " " << K;
return ;
}
if (N == 2)
{
cout << 0 << " " << K;
return ;
}
int P = N - 2;
int Q = N - 1;
int VAL = 0;
for ( int i = 1; i <= (N - 3); i++)
{
cout << " " << i;
VAL ^= i;
}
if (VAL == K)
{
while ( (P ^ Q) <= P)
{ Q++;
}
cout << " " << P << " " << Q
<< " " << (P ^ Q);
}
else
{
while ( (P ^ K ^ VAL) <= N-3)
{ P++;
}
cout << " " << 0 << " " << P
<< " " << (P ^ K ^ VAL);
}
}
int main()
{
int N = 4, X = 6;
findArray(N, X);
return 0;
}
|
C
#include <stdio.h>
void findArray( int N, int K)
{
if (N == 1) {
printf ( "%d" , K);
return ;
}
if (N == 2) {
printf ( "%d %d" , 0, K);
return ;
}
int P = N - 2;
int Q = N - 1;
int VAL = 0;
for ( int i = 1; i <= (N - 3); i++) {
printf ( "%d " , i);
VAL ^= i;
}
if (VAL == K) {
while ( (P ^ Q) <= P)
{ Q++;
}
printf ( "%d %d %d" , P, Q, P ^ Q);
}
else {
while ( (P ^ K ^ VAL) <= N-2)
{ P++;
}
printf ( "%d %d %d" , 0,
P, P ^ K ^ VAL);
}
}
int main()
{
int N = 4, X = 6;
findArray(N, X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findArray( int N, int K)
{
if (N == 1 )
{
System.out.print(K + " " );
return ;
}
if (N == 2 )
{
System.out.print( 0 + " " + K);
return ;
}
int P = N - 2 ;
int Q = N - 1 ;
int VAL = 0 ;
for ( int i = 1 ; i <= (N - 3 ); i++)
{
System.out.print(i + " " );
VAL ^= i;
}
if (VAL == K)
{
while ( (P ^ Q) <= P)
{ Q++;
}
System.out.print(P + " " +
Q + " " + (P ^ Q));
}
else
{
while ( (P ^ K ^ VAL) <= N- 2 )
{ P++;
}
System.out.print( 0 + " " +
P + " " +
(P ^ K ^ VAL));
}
}
public static void main(String[] args)
{
int N = 4 , X = 6 ;
findArray(N, X);
}
}
|
Python3
def findArray(N, K):
if (N = = 1 ):
print (K, end = " " )
return
if (N = = 2 ):
print ( "0" , end = " " )
print (K, end = " " )
return
P = N - 2
Q = N - 1
VAL = 0
for i in range ( 1 , N - 2 ):
print (i, end = " " )
VAL ^ = i
if (VAL = = K):
while ((P ^ Q) < = P):
Q = Q + 1
print (P, end = " " )
print (Q, end = " " )
print (P ^ Q, end = " " )
else :
while ((P ^ K ^ VAL) < = N - 2 ):
P = P + 1
print ( "0" , end = " " )
print (P, end = " " )
print (P ^ K ^ VAL, end = " " )
N = 4
X = 6
findArray(N, X)
|
C#
using System;
class GFG{
static void findArray( int N, int K)
{
if (N == 1) {
Console.Write(K + " " );
return ;
}
if (N == 2) {
Console.Write(0 + " " + K);
return ;
}
int P = N - 2;
int Q = N - 1;
int VAL = 0;
for ( int i = 1; i <= (N - 3); i++) {
Console.Write(i + " " );
VAL ^= i;
}
if (VAL == K) {
while ( (P ^ Q) <= P)
{ Q++;
}
Console.Write(P + " " + Q + " " + (P ^ Q));
}
else {
while ( (P ^ K ^ VAL) <= N-2)
{ P++;
}
Console.Write(0 + " " + P + " " + (P ^ K ^ VAL));
}
}
public static void Main()
{
int N = 4, X = 6;
findArray(N, X);
}
}
|
Javascript
<script>
function findArray(N, K)
{
if (N == 1)
{
document.write(K + " " );
return ;
}
if (N == 2)
{
document.write(0 + " " + K);
return ;
}
let P = N - 2;
let Q = N - 1;
let VAL = 0;
for (let i = 1; i <= (N - 3); i++)
{
document.write(i + " " );
VAL ^= i;
}
if (VAL == K)
{
while ( (P ^ Q) <= P)
{ Q++;
}
document.write(P + " " +
Q + " " + (P ^ Q));
}
else
{
while ( (P ^ K ^ VAL) <= N-2)
{ P++;
}
document.write(0 + " " +
P + " " +
(P ^ K ^ VAL));
}
}
let N = 4, X = 6;
findArray(N, X);
</script>
|
Output:
1 0 2 5
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...