![]() |
VOOZH | about |
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:
Below is the implementation of the above approach:
1 10 3 1 3
Time Complexity: O(N)
Auxiliary Space: O(1)