Adjacent xor code in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given a sequence arr[] of N-1 elements which is xor of all adjacent pairs in an array, the task is to find that original array from the arr[].
    Note: It is given that the N is always odd and arr[] contains the permutation of N natural number.
    Examples: 
     

    Input: arr[] = {3, 1} 
    Output: 1 2 3 
    Explanation: 
    The XOR of the output array will lead to the given array that is: 
    1 ^ 2 = 3 
    2 ^ 3 = 1
    Input: arr[] = {7, 5, 3, 7} 
    Output: 3 4 1 2 5 
    Explanation: 
    The XOR of the output array will lead to the given array that is: 
    3 ^ 4 = 7 
    4 ^ 1 = 5 
    1 ^ 2 = 3 
    2 ^ 5 = 7 
     

    Approach: 
     

    1. The idea is to find the XOR of all the elements of the 1 to N and the xor of the adjacent elements of the given array to find the last element of the expected array.
    2. As the XOR of adjacent elements will contain all the elements except the last element, then the XOR of this with all the numbers from 1 to N will give the last element of the expected permutation. 
      For Example: 
       
    Let's the expected array be - {a, b, c, d, e}
    Then the XOR array for this array will be - 
    {a^b, b^c, c^d, d^e}
    
    Now XOR of all the element from 1 to N -
    xor_all => a ^ b ^ c ^ d ^ e
    
    XOR of the adjacent elements -
    xor_adjacent => ((a ^ b) ^ (c ^ d))
    
    Now the XOR of the both the array will be the 
    last element of the expected permutation 
    => (a ^ b ^ c ^ d ^ e) ^ ((a ^ b) ^ (c ^ d))
    => As all elements are in pair except the last element.
    => (a ^ a ^ b ^ b ^ c ^ c ^ d ^ d ^ e)
    => (0 ^ 0 ^ 0 ^ 0 ^ e)
    => e
    1. Now for the rest of the element, continuously, on xor of this last element we will get last second element, i.e, d.
    2. Repeatedly, Update the last element and finally get the first element, i.e, a.

    Below is the implementation of the above approach:
     

    C++

    #include

    using namespace std;

    int xor_all_elements(int n)

    {

        switch (n & 3) {

        case 0:

            return n;

        case 1:

            return 1;

        case 2:

            return n + 1;

        case 3:

            return 0;

        }

    }

    vector<int> findArray(int xorr[], int n)

    {

        vector<int> arr;

        int xor_all = xor_all_elements(n);

        int xor_adjacent = 0;

        for (int i = 0; i < n - 1; i += 2) {

            xor_adjacent = xor_adjacent ^ xorr[i];

        }

        int last_element = xor_all ^ xor_adjacent;

        arr.push_back(last_element);

        for (int i = n - 2; i >= 0; i--) {

            last_element = xorr[i] ^ last_element;

            arr.push_back(last_element);

        }

        return arr;

    }

    int main()

    {

        vector<int> arr;

        int xorr[] = { 7, 5, 3, 7 };

        int n = 5;

        arr = findArray(xorr, n);

        for (int i = n - 1; i >= 0; i--) {

            cout << arr[i] << " ";

        }

    }

    Java

    import java.util.*;

    class GFG{

    static int xor_all_elements(int n)

    {

        switch (n & 3) {

        case 0:

            return n;

        case 1:

            return 1;

        case 2:

            return n + 1;

        }

        return 0;

    }

    static Vector findArray(int xorr[], int n)

    {

        Vector arr = new Vector();

        int xor_all = xor_all_elements(n);

        int xor_adjacent = 0;

        for (int i = 0; i < n - 1; i += 2) {

            xor_adjacent = xor_adjacent ^ xorr[i];

        }

        int last_element = xor_all ^ xor_adjacent;

        arr.add(last_element);

        for (int i = n - 2; i >= 0; i--)

        {

            last_element = xorr[i] ^ last_element;

            arr.add(last_element);

        }

        return arr;

    }

    public static void main(String[] args)

    {

        Vector arr = new Vector();

        int xorr[] = { 7, 5, 3, 7 };

        int n = 5;

        arr = findArray(xorr, n);

        for (int i = n - 1; i >= 0; i--)

        {

            System.out.print(arr.get(i)+ " ");

        }

    }

    }

    Python3

    def xor_all_elements(n):

        if n & 3 == 0:

            return n

        elif n & 3 == 1:

            return 1

        elif n & 3 == 2:

            return n + 1

        else:

            return 0

    def findArray(xorr, n):

        arr = []

        xor_all = xor_all_elements(n)

        xor_adjacent = 0

        for i in range(0, n - 1, 2):

            xor_adjacent = xor_adjacent ^ xorr[i]

        last_element = xor_all ^ xor_adjacent

        arr.append(last_element)

        for i in range(n - 2, -1, -1):

            last_element = xorr[i] ^ last_element

            arr.append(last_element)

        return arr

    xorr = [7, 5, 3, 7]

    n = 5

    arr = findArray(xorr, n)

    for i in range(n - 1, -1, -1):

        print(arr[i], end=" ")

    C#

    using System;

    using System.Collections.Generic;

    class GFG{

    static int xor_all_elements(int n)

    {

        switch (n & 3) {

        case 0:

            return n;

        case 1:

            return 1;

        case 2:

            return n + 1;

        }

        return 0;

    }

    static List<int> findArray(int []xorr, int n)

    {

        List<int> arr = new List<int>();

        int xor_all = xor_all_elements(n);

        int xor_adjacent = 0;

        for (int i = 0; i < n - 1; i += 2) {

            xor_adjacent = xor_adjacent ^ xorr[i];

        }

        int last_element = xor_all ^ xor_adjacent;

        arr.Add(last_element);

        for (int i = n - 2; i >= 0; i--)

        {

            last_element = xorr[i] ^ last_element;

            arr.Add(last_element);

        }

        return arr;

    }

    public static void Main(String[] args)

    {

        List<int> arr = new List<int>();

        int []xorr = { 7, 5, 3, 7 };

        int n = 5;

        arr = findArray(xorr, n);

        for (int i = n - 1; i >= 0; i--)

        {

            Console.Write(arr[i]+ " ");

        }

    }

    }

    Javascript

    Performance Analysis: 
     

    • Time Complexity: In the above approach we iterate over the entire xor array to find the XOR of the Adjacent elements, then the complexity in the worst case will be O(N)
    • Space Complexity: In the above approach there is a vector array used to store the Permutation of the numbers from 1 to N, then the space complexity will be O(N)

    How do you find the XOR of consecutive elements in an array?

    Let a, b, c, d, e, f are the original elements, and the xor of every 2 consecutive elements is given, i.e a^b = k1, b ^ c = k2, c ^ d = k3, d ^ e = k4, e ^ f = k5 (where k1, k2, k3, k4, k5 are the elements that are given us along with the first element a), and we have to find the value of b, c, d, e, f.

    How do you find the XOR of two arrays?

    Therefore, the following steps are followed to compute the answer: Create a variable to store the XOR of the array as a result. For each element in the array, find the XOR of the element and the result variable using '^' operator. Finally, the result variable stores the XOR of all elements in the array.

    How do you do XOR between two numbers?

    To find the XOR of two numbers, follow these instructions:.
    Convert the numbers into the binary representation..
    Compare the corresponding bits of the two numbers..
    If only one of the input bits is true (1), the output is true (1). Otherwise, the output is false (0)..

    How does Python calculate XOR?

    In Python, bitwise operators are used to performing bitwise calculations on integers. The integers are first converted into binary and then operations are performed on bit by bit, hence the name bitwise operators. ... Bitwise operators..