Create a sequence whose XOR of elements is y
Given two integers N and Y, the task is to generate a sequence of N distinct non-negative integers whose bitwise-XOR of all the elements of this generated sequence is equal to Y i.e. A1 ^ A2 ^ A3 ^ ….. ^ AN = Y where ^ denotes bitwise XOR. if no such sequence is possible then print -1.
Examples:
Input: N = 4, Y = 3
Output: 1 131072 131074 0
(1 ^ 131072 ^ 131074 ^ 0) = 3 and all four elements are distinct.
Input: N = 10, Y = 6
Output: 1 2 3 4 5 6 7 131072 131078 0
Approach: This is a constructive problem and may contain multiple solutions. Follow the below steps to generate the required sequence:
- Take first N – 3 elements as part of the sequence i.e. 1, 2, 3, 4, …, (N – 3)
- Let the XOR of the chosen elements be x and num be an integer that has not been chosen yet. Now there are two cases:
- If x = y then we can add num, num * 2 and (num ^ (num * 2)) to the last 3 remaining numbers because num ^ (num * 2) ^ (num ^ (num * 2)) = 0 and x ^ 0 = x
- If x != y then we can add 0, num and (num ^ x ^ y) because 0 ^ num ^ (num ^ x ^ y) = x ^ y and x ^ x ^ y = y
Note: Sequence is not possible when N = 2 and Y = 0 because this condition can only be satisfied by two equal numbers which is not allowed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void Findseq( int n, int x)
{
const int pw1 = (1 << 17);
const int pw2 = (1 << 18);
if (n == 1)
cout << x << endl;
else if (n == 2 && x == 0)
cout << "-1" << endl;
else if (n == 2)
cout << x << " "
<< "0" << endl;
else {
int i;
int ans = 0;
for (i = 1; i <= n - 3; i++) {
cout << i << " " ;
ans = ans ^ i;
}
if (ans == x)
cout << pw1 + pw2 << " "
<< pw1 << " " << pw2 << endl;
else
cout << pw1 << " " << ((pw1 ^ x) ^ ans)
<< " 0 " << endl;
}
}
int main()
{
int n = 4, x = 3;
Findseq(n, x);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void Findseq( int n, int x)
{
int pw1 = 1 << 17 ;
int pw2 = ( 1 << 18 );
if (n == 1 ) {
System.out.println(x);
}
else if (n == 2 && x == 0 ) {
System.out.println( "-1" );
}
else if (n == 2 ) {
System.out.println(x + " "
+ "" );
}
else {
int i;
int ans = 0 ;
for (i = 1 ; i <= n - 3 ; i++) {
System.out.print(i + " " );
ans = ans ^ i;
}
if (ans == x) {
System.out.println(pw1 + pw2 + " " + pw1 + " " + pw2);
}
else {
System.out.println(pw1 + " " + ((pw1 ^ x) ^ ans)
+ " 0 " );
}
}
}
public static void main(String[] args)
{
int n = 4 , x = 3 ;
Findseq(n, x);
}
}
|
Python3
def Findseq(n, x) :
pw1 = ( 1 << 17 );
pw2 = ( 1 << 18 );
if (n = = 1 ) :
print (x);
elif (n = = 2 and x = = 0 ) :
print ( "-1" );
elif (n = = 2 ) :
print (x, " " , "0" );
else :
ans = 0 ;
for i in range ( 1 , n - 2 ) :
print (i, end = " " );
ans = ans ^ i;
if (ans = = x) :
print (pw1 + pw2, " " , pw1, " " , pw2);
else :
print (pw1, " " , ((pw1 ^ x) ^ ans), " 0 " );
if __name__ = = "__main__" :
n = 4 ; x = 3 ;
Findseq(n, x);
|
C#
using System;
class GFG
{
static void Findseq( int n, int x)
{
int pw1 = 1 << 17;
int pw2 = (1 << 18);
if (n == 1)
{
Console.WriteLine(x);
}
else if (n == 2 && x == 0)
{
Console.WriteLine( "-1" );
}
else if (n == 2)
{
Console.WriteLine(x + " "
+ "" );
}
else
{
int i;
int ans = 0;
for (i = 1; i <= n - 3; i++)
{
Console.Write(i + " " );
ans = ans ^ i;
}
if (ans == x)
{
Console.WriteLine(pw1 + pw2 + " " + pw1 + " " + pw2);
}
else
{
Console.WriteLine(pw1 + " " + ((pw1 ^ x) ^ ans)
+ " 0 " );
}
}
}
public static void Main()
{
int n = 4, x = 3;
Findseq(n, x);
}
}
|
PHP
<?php
function Findseq( $n , $x )
{
$pw1 = (1 << 17);
$pw2 = (1 << 18);
if ( $n == 1)
echo $x , "\n" ;
else if ( $n == 2 && $x == 0)
echo "-1" , "\n" ;
else if ( $n == 2)
echo $x , " " ,
"0" , "\n" ;
else
{
$i ;
$ans = 0;
for ( $i = 1; $i <= $n - 3; $i ++)
{
echo $i , " " ;
$ans = $ans ^ $i ;
}
if ( $ans == $x )
echo ( $pw1 + $pw2 ) , " " ,
$pw1 , " " , $pw2 , "\n" ;
else
echo $pw1 , " " ,(( $pw1 ^ $x ) ^ $ans ),
" 0 " , "\n" ;
}
}
$n = 4;
$x = 3;
Findseq( $n , $x );
?>
|
Javascript
<script>
function Findseq(n, x)
{
let pw1 = (1 << 17);
let pw2 = (1 << 18);
if (n == 1)
document.write(x + "<br>" );
else if (n == 2 && x == 0)
document.write( "-1<br>" );
else if (n == 2)
document.write(x + " "
+ "0" + "<br>" );
else {
let i;
let ans = 0;
for (i = 1; i <= n - 3; i++) {
document.write(i + " " );
ans = ans ^ i;
}
if (ans == x)
document.write((pw1 + pw2) + " "
+ pw1 + " " <+ pw2 + "<br>" );
else
document.write(pw1 + " " + ((pw1 ^ x) ^ ans)
+ " 0 " + "<br>" );
}
}
let n = 4, x = 3;
Findseq(n, x)
</script>
|
Output:
1 131072 131074 0
Time Complexity: O(N), as we are using a loop to traverse N times.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...