Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmCreate a sequence whose XOR of elements is y

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: 
 

  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 <bits/stdc++.h>
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




<?php
// PHP implementation of the approach
// Function to find and print
// the required sequence
function 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..
?>


Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to find and print
// the required sequence
function 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>


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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments