# Find array whose elements are XOR of adjacent elements in given array

• Last Updated : 14 Sep, 2021

Given an array arr[] consisting of N integers, the task is to re-construct an array arr[] such that the values in arr[] are obtained by doing XOR of the adjacent elements in the array. Print the array elements.

Examples:

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.

Input: arr[ ] = {10, 11, 1, 2, 3}
Output: 1 10 3 1 3
Explanation:
At index 0, arr xor arr = 1
At index 1, arr xor arr = 10
At index 2, arr xor arr = 3

At index 4, No element is left So, it will remain as it is.
New Array will be {1, 10, 3, 1, 3}

Input: arr[ ] = {5, 9, 7, 6}
Output: 12 14 1 6
Explanation:
At index 0, arr xor arr = 12
At index 1, arr xor arr = 14
At index 2, arr xor arr = 1
At index 3, No element is left So, it will remain as it is.
New Array will be {12, 14, 1, 6}

Approach: The main idea to solve the given problem is to perform the following steps:

1. Traverse the given array arr[] from the 0th index to (N – 2)th index.
2. For each element arr[i] at ith position calculate arr[i] ^ arr[i+1] and store it at position i.

Below is the implementation of the above approach:

## C++

 `// C++ implementation``// of the above approach``#include ``using` `namespace` `std;` `// Function to reconstruct the array``// arr[] with xor of adjacent elements``int``* game_with_number(``int` `arr[], ``int` `n)``{``    ``// Iterate through each element``    ``for` `(``int` `i = 0; i < n - 1; i++) {``        ``// Store the xor of current``        ``// and next element in arr[i]``        ``arr[i] = arr[i] ^ arr[i + 1];``    ``}` `    ``return` `arr;``}` `// Function to print the array``void` `print(``int` `arr[], ``int` `n)``{``    ``for` `(``int` `i = 0; i < n; i++) {``        ``cout << arr[i] << ``" "``;``    ``}``}` `// Driver Code``int` `main()``{``    ``// Inputs``    ``int` `arr[] = { 10, 11, 1, 2, 3 };` `    ``// Length of the array given``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);` `    ``// Function call to reconstruct the arr[]``    ``int``* new_arr = game_with_number(arr, n);` `    ``// Function call to print arr[]``    ``print(new_arr, n);``}`

## Java

 `// Java implementation``// of the above approach``import` `java.io.*;` `class` `GFG{` `// Function to reconstruct the array``// arr[] with xor of adjacent elements``static` `int``[] game_with_number(``int` `arr[], ``int` `n)``{``    ` `    ``// Iterate through each element``    ``for``(``int` `i = ``0``; i < n - ``1``; i++)``    ``{``        ` `        ``// Store the xor of current``        ``// and next element in arr[i]``        ``arr[i] = arr[i] ^ arr[i + ``1``];``    ``}``    ``return` `arr;``}` `// Function to print the array``static` `void` `print(``int` `arr[], ``int` `n)``{``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ``System.out.print(arr[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Inputs``    ``int` `arr[] = { ``10``, ``11``, ``1``, ``2``, ``3` `};` `    ``// Length of the array given``    ``int` `n = arr.length;` `    ``// Function call to reconstruct the arr[]``    ``int``[] new_arr = game_with_number(arr, n);` `    ``// Function call to print arr[]``    ``print(new_arr, n);``}``}` `// This code is contributed by subhammahato348`

## Python3

 `# Python3 implementation``# of the above approach` `# Function to reconstruct the array``# arr[] with xor of adjacent elements``def` `game_with_number(arr, n):``    ``# Iterate through each element``    ``for` `i ``in` `range``(n``-``1``):``      ` `        ``# Store the xor of current``        ``#and next element in arr[i]``        ``arr[i] ``=` `arr[i] ^ arr[i ``+` `1``]` `    ``return` `arr` `# Function to print array``def` `printt(arr, n):``    ``print``(``*``arr)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``# Inputs``    ``arr``=` `[``10``, ``11``, ``1``, ``2``, ``3``]` `    ``# Length of the array given``    ``n ``=` `len``(arr)` `    ``# Function call to reconstruct the arr[]``    ``new_arr ``=` `game_with_number(arr, n);` `    ``# Function call to prarr[]``    ``printt(new_arr, n)` `    ``# This code is contributed by mohit kumar 29.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to reconstruct the array``// arr[] with xor of adjacent elements``static` `int``[] game_with_number(``int``[] arr, ``int` `n)``{``    ` `    ``// Iterate through each element``    ``for``(``int` `i = 0; i < n - 1; i++)``    ``{``        ` `        ``// Store the xor of current``        ``// and next element in arr[i]``        ``arr[i] = arr[i] ^ arr[i + 1];``    ``}``    ``return` `arr;``}` `// Function to print the array``static` `void` `print(``int``[] arr, ``int` `n)``{``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``Console.Write(arr[i] + ``" "``);``    ``}``}` `// Driver Code``public` `static` `void` `Main()``{``    ``// Inputs``    ``int``[] arr = { 10, 11, 1, 2, 3 };` `    ``// Length of the array given``    ``int` `n = arr.Length;` `    ``// Function call to reconstruct the arr[]``    ``int``[] new_arr = game_with_number(arr, n);` `    ``// Function call to print arr[]``    ``print(new_arr, n);``}``}` `// This code is contributed by target_2.`

## Javascript

 `    ```
Output
`1 10 3 1 3 `

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up