# 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:

Input: arr[ ] = {10, 11, 1, 2, 3}
Output: 1 10 3 1 3
Explanation:
At index 0, arr[0] xor arr[1] = 1
At index 1, arr[1] xor arr[2] = 10
At index 2, arr[2] xor arr[3] = 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[0] xor arr[1] = 12
At index 1, arr[1] xor arr[2] = 14
At index 2, arr[2] xor arr[3] = 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[0]);`   `    ``// 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)

Previous
Next