# Find elements of array using XOR of consecutive elements

Given an array arr[] in which XOR of every 2 consecutive elements of the original array is given i.e if the total number of elements in the original array is then the size of this XOR array would be n-1. The first element in the original array is also given. The task is to find out the rest of n-1 elements of the original 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 as along with the first element a), and we have to find the value of b, c, d, e, f.

Examples:

```Input : arr[] = {13, 2, 6, 1}, a = 5
Output : 5 8 10 12 13
5^8=13, 8^10=2, 10^12=6, 12^13=1

Input : arr[] = {12, 5, 26, 7}, a = 6
Output : 6 10 15 21 18
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: We can find all the elements one by one with the help of (first elements), and to find the next element i.e we have to xor a by arr, similarly for xor arr with b and so on.

This works by following the properties of XOR as stated below:

• XOR of a number to itself is zero.
• XOR of a number with zero given the number itself.

So, as arr contains a^b. Therefore,

```a ^ arr = a ^ a ^ b
= 0 ^ b
= b
```

Similarly as arr[i] contains XOR of ai and ai+1. Therefore,

```ai ^ arr[i] = ai ^ ai ^ ai+1
= 0 ^ ai+1
= ai+1
```

Below is the implementation of above approach

## C++

 `// CPP program to find the array elements ` `// using XOR of consecutive elements ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the array elements ` `// using XOR of consecutive elements ` `void` `getElements(``int` `a, ``int` `arr[], ``int` `n) ` `{ ` `    ``// array to store the orginal ` `    ``// elements ` `    ``int` `elements[n + 1]; ` ` `  `    ``// first element a i.e elements=a ` `    ``elements = a; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``/*  To get the next elements we have to calculate  ` `            ``xor of previous elements with given xor of 2  ` `            ``consecutive elements. ` `            ``e.g. if a^b=k1 so to get b xor a both side. ` `            ``b = k1^a  ` `        ``*/` `        ``elements[i + 1] = arr[i] ^ elements[i]; ` `    ``} ` ` `  `    ``// Printing the original array elements ` `    ``for` `(``int` `i = 0; i < n + 1; i++) ` `        ``cout << elements[i] << ``" "``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 13, 2, 6, 1 }; ` ` `  `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``int` `a = 5; ` ` `  `    ``getElements(a, arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java  program to find the array elements ` `// using XOR of consecutive elements ` ` `  `import` `java.io.*; ` ` `  `class` `GFG { ` `    `  ` `  `// Function to find the array elements ` `// using XOR of consecutive elements ` `static` `void` `getElements(``int` `a, ``int` `arr[], ``int` `n) ` `{ ` `    ``// array to store the orginal ` `    ``// elements ` `    ``int` `elements[] = ``new` `int``[n + ``1``]; ` ` `  `    ``// first element a i.e elements=a ` `    ``elements[``0``] = a; ` ` `  `    ``for` `(``int` `i = ``0``; i < n; i++) { ` ` `  `        ``/* To get the next elements we have to calculate  ` `            ``xor of previous elements with given xor of 2  ` `            ``consecutive elements. ` `            ``e.g. if a^b=k1 so to get b xor a both side. ` `            ``b = k1^a  ` `        ``*/` `        ``elements[i + ``1``] = arr[i] ^ elements[i]; ` `    ``} ` ` `  `    ``// Printing the original array elements ` `    ``for` `(``int` `i = ``0``; i < n + ``1``; i++) ` `        ``System.out.print( elements[i] + ``" "``); ` `} ` ` `  `// Driver Code ` ` `  `    ``public` `static` `void` `main (String[] args) { ` `            ``int` `arr[] = { ``13``, ``2``, ``6``, ``1` `}; ` ` `  `    ``int` `n = arr.length; ` ` `  `    ``int` `a = ``5``; ` ` `  `    ``getElements(a, arr, n); ` `    ``} ` `} ` `// This code is contributed by anuj_67.. `

## Python3

 `# Python3 program to find the array  ` `# elements using xor of consecutive elements ` ` `  `# Function to find the array elements ` `# using XOR of consecutive elements ` ` `  `def` `getElements(a, arr, n): ` `     `  `    ``# array to store the original elements ` `    ``elements ``=` `[``1` `for` `i ``in` `range``(n ``+` `1``)] ` `     `  `    ``# first elements a i.e elements=a ` `    ``elements[``0``] ``=` `a ` `     `  `    ``for` `i ``in` `range``(n): ` `         `  `        ``# To get the next elements we have to  ` `        ``# calculate xor of previous elements  ` `        ``# with given xor of 2 consecutive elements. ` `        ``# e.g. if a^b=k1 so to get b xor a both side. ` `        ``# b = k1^a  ` `        ``elements[i ``+` `1``] ``=` `arr[i] ^ elements[i] ` `             `  `    ``# Printing the original array elements ` `    ``for` `i ``in` `range``(n ``+` `1``): ` `        ``print``(elements[i], end ``=` `" "``) ` ` `  `# Driver code ` `arr ``=` `[``13``, ``2``, ``6``, ``1``] ` `n ``=` `len``(arr) ` `a ``=` `5` `getElements(a, arr, n) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# program to find the array elements  ` `// using XOR of consecutive elements  ` ` `  `using` `System; ` ` `  `class` `GFG {  ` `    ``// Function to find the array elements  ` `    ``// using XOR of consecutive elements  ` `    ``static` `void` `getElements(``int` `a, ``int` `[]arr, ``int` `n)  ` `    ``{  ` `        ``// array to store the orginal  ` `        ``// elements  ` `        ``int` `[]elements = ``new` `int``[n + 1];  ` `     `  `        ``// first element a i.e elements=a  ` `        ``elements = a;  ` `     `  `        ``for` `(``int` `i = 0; i < n; i++) {  ` `     `  `            ``/* To get the next elements we have to calculate  ` `                ``xor of previous elements with given xor of 2  ` `                ``consecutive elements.  ` `                ``e.g. if a^b=k1 so to get b xor a both side.  ` `                ``b = k1^a  ` `            ``*/` `            ``elements[i + 1] = arr[i] ^ elements[i];  ` `        ``}  ` `     `  `        ``// Printing the original array elements  ` `        ``for` `(``int` `i = 0; i < n + 1; i++)  ` `            ``Console.Write( elements[i] + ``" "``);  ` `    ``}  ` ` `  `    ``// Driver Code  ` `    ``public` `static` `void` `Main () {  ` `            ``int` `[]arr = { 13, 2, 6, 1 };  ` ` `  `            ``int` `n = arr.Length;  ` ` `  `            ``int` `a = 5;  ` ` `  `            ``getElements(a, arr, n);  ` `    ``}  ` `        ``// This code is contributed by Ryuga ` `}  `

## PHP

 ` `

Output:

```5 8 10 12 13
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.