Skip to content

# Create a sequence whose XOR of elements is y

• Last Updated : 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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Approach: This is a constructive problem and may contain multiple solutions. Follow the below steps to generate the required sequence:

1. Take first N – 3 elements as part of the sequence i.e. 1, 2, 3, 4, …, (N – 3)
2. 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++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;` `// Function to find and print``// the required sequence``void` `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 code``int` `main()``{``    ``int` `n = 4, x = 3;``    ``Findseq(n, x);` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `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

 `# Python3 implementation of the approach` `# Function to find and print``# the required sequence``def` `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 code``if` `__name__ ``=``=` `"__main__"` `:``    ` `    ``n ``=` `4``; x ``=` `3``;``    ``Findseq(n, x);``    ` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `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

 ``

## Javascript

 ``
Output:
`1 131072 131074 0`

Time Complexity: O(N)

My Personal Notes arrow_drop_up