Create a sequence whose XOR of elements is y - GeeksforGeeks

27 Apr, 2021
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 = xIf 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
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 = xIf 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
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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to find and print// the required sequencevoid Findseq(int n, int x){    const int pw1 = (1 << 17);    const int pw2 = (1 << 18);     // Base case    if (n == 1)        cout << x << endl;     // Not allowed case    else if (n == 2 && x == 0)        cout << "-1" << endl;    else if (n == 2)        cout << x << " "             << "0" << endl;    else {        int i;        int ans = 0;         // XOR of first N - 3 elements        for (i = 1; i <= n - 3; i++) {            cout << i << " ";            ans = ans ^ i;        }         // Case 1: Add three integers whose XOR is 0        if (ans == x)            cout << pw1 + pw2 << " "                 << pw1 << " " << pw2 << endl;         // Case 2: Add three integers        // whose XOR is equal to ans        else            cout << pw1 << " " << ((pw1 ^ x) ^ ans)                 << " 0 " << endl;    }} // Driver codeint main(){    int n = 4, x = 3;    Findseq(n, x);     return 0;}
// Java implementation of the approachimport java.util.*; class GFG {     // Function to find and print    // the required sequence    static void Findseq(int n, int x)    {        int pw1 = 1 << 17;        int pw2 = (1 << 18);         // Base case        if (n == 1) {            System.out.println(x);        }         // Not allowed case        else if (n == 2 && x == 0) {            System.out.println("-1");        }        else if (n == 2) {            System.out.println(x + " "                               + "");        }        else {            int i;            int ans = 0;             // XOR of first N - 3 elements            for (i = 1; i <= n - 3; i++) {                System.out.print(i + " ");                ans = ans ^ i;            }             // Case 1: Add three integers whose XOR is 0            if (ans == x) {                System.out.println(pw1 + pw2 + " " + pw1 + " " + pw2);            }             // Case 2: Add three integers            // whose XOR is equal to ans            else {                System.out.println(pw1 + " " + ((pw1 ^ x) ^ ans)                                   + " 0 ");            }        }    }     // Driver code    public static void main(String[] args)    {        int n = 4, x = 3;        Findseq(n, x);    }} // This code contributed by Rajput-Ji
# Python3 implementation of the approach # Function to find and print# the required sequencedef Findseq(n, x) :         pw1 = (1 << 17);    pw2 = (1 << 18);     # Base case    if (n == 1) :        print(x);     # Not allowed case    elif (n == 2 and x == 0) :        print("-1");             elif (n == 2) :        print(x, " ", "0");             else :             ans = 0;         # XOR of first N - 3 elements        for i in range(1, n - 2) :            print(i, end = " ");            ans = ans ^ i;                 # Case 1: Add three integers whose XOR is 0        if (ans == x) :            print(pw1 + pw2, " ", pw1, " ", pw2);         # Case 2: Add three integers        # whose XOR is equal to ans        else :            print(pw1, " ", ((pw1 ^ x) ^ ans), " 0 "); # Driver codeif __name__ == "__main__" :         n = 4; x = 3;    Findseq(n, x);     # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{     // Function to find and print    // the required sequence    static void Findseq(int n, int x)    {        int pw1 = 1 << 17;        int pw2 = (1 << 18);         // Base case        if (n == 1)        {            Console.WriteLine(x);        }         // Not allowed case        else if (n == 2 && x == 0)        {            Console.WriteLine("-1");        }        else if (n == 2)        {            Console.WriteLine(x + " "                            + "");        }        else        {            int i;            int ans = 0;             // XOR of first N - 3 elements            for (i = 1; i <= n - 3; i++)            {                Console.Write(i + " ");                ans = ans ^ i;            }             // Case 1: Add three integers whose XOR is 0            if (ans == x)            {                Console.WriteLine(pw1 + pw2 + " " + pw1 + " " + pw2);            }             // Case 2: Add three integers            // whose XOR is equal to ans            else            {                Console.WriteLine(pw1 + " " + ((pw1 ^ x) ^ ans)                                + " 0 ");            }        }    }     // Driver code    public static void Main()    {        int n = 4, x = 3;        Findseq(n, x);    }} // This code contributed by anuj_67..
<?php// PHP implementation of the approach// Function to find and print// the required sequencefunction Findseq($n, $x){    $pw1 = (1 << 17);    $pw2 = (1 << 18);     // Base case    if ($n == 1)        echo $x ,"\n";     // Not allowed case    else if ($n == 2 && $x == 0)        echo "-1" ,"\n";    else if ($n == 2)        echo $x ," ",            "0","\n";    else    {        $i;        $ans = 0;         // XOR of first N - 3 elements        for ($i = 1; $i <= $n - 3; $i++)        {            echo $i , " ";            $ans = $ans ^ $i;        }         // Case 1: Add three integers whose XOR is 0        if ($ans == $x)            echo ($pw1 + $pw2) , " ",                $pw1 ," " , $pw2,"\n";         // Case 2: Add three integers        // whose XOR is equal to ans        else            echo $pw1 , " " ,(($pw1 ^ $x) ^ $ans),                " 0 " ,"\n";    }} // Driver code     $n = 4;    $x = 3;    Findseq($n, $x); // This code is contributed BY @Tushil..?>
<script> // Javascript implementation of the approach // Function to find and print// the required sequencefunction Findseq(n, x){    let pw1 = (1 << 17);    let pw2 = (1 << 18);     // Base case    if (n == 1)        document.write(x + "<br>");     // Not allowed case    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;         // XOR of first N - 3 elements        for (i = 1; i <= n - 3; i++) {            document.write(i + " ");            ans = ans ^ i;        }         // Case 1: Add three integers whose XOR is 0        if (ans == x)            document.write((pw1 + pw2) + " "                 + pw1 + " " <+ pw2 + "<br>");         // Case 2: Add three integers        // whose XOR is equal to ans        else            document.write(pw1 + " " + ((pw1 ^ x) ^ ans)                 + " 0 " + "<br>");    }} // Driver code    let n = 4, x = 3;    Findseq(n, x) </script>
1 131072 131074 0
 
Time Complexity: O(N) 
ankthon
Rajput-Ji
vt_m
jit_t
souravmahato348
Bitwise-XOR
Constructive Algorithms
Algorithms
Arrays
Arrays
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
SCAN (Elevator) Disk Scheduling Algorithms
Rail Fence Cipher - Encryption and Decryption
Quadratic Probing in Hashing
Program for SSTF disk scheduling algorithm
Arrays in Java
Maximum and minimum of an array using minimum number of comparisons
Arrays in C/C++
Write a program to reverse an array or string
Program for array rotation