Find array whose elements are XOR of adjacent elements in given array - GeeksforGeeks

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] = 1At index 1, arr[1] xor arr[2] = 10At 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] = 12At index 1, arr[1] xor arr[2] = 14At index 2, arr[2] xor arr[3] = 1At 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:
Traverse the given array arr[] from the 0th index to (N – 2)th index.For each element arr[i] at ith position calculate arr[i] ^ arr[i+1] and store it at position i.
Traverse the given array arr[] from the 0th index to (N – 2)th index.
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++
Java
Python3
C#
Javascript
// C++ implementation// of the above approach#include <iostream>using namespace std; // Function to reconstruct the array// arr[] with xor of adjacent elementsint* 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 arrayvoid print(int arr[], int n){    for (int i = 0; i < n; i++) {        cout << arr[i] << " ";    }} // Driver Codeint 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 implementation// of the above approachimport java.io.*; class GFG{ // Function to reconstruct the array// arr[] with xor of adjacent elementsstatic 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 arraystatic void print(int arr[], int n){    for(int i = 0; i < n; i++)    {        System.out.print(arr[i] + " ");    }} // Driver Codepublic 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 implementation# of the above approach # Function to reconstruct the array# arr[] with xor of adjacent elementsdef 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 arraydef printt(arr, n):    print(*arr) # Driver Codeif __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# program for the above approachusing System; class GFG{ // Function to reconstruct the array// arr[] with xor of adjacent elementsstatic 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 arraystatic void print(int[] arr, int n){    for(int i = 0; i < n; i++)    {        Console.Write(arr[i] + " ");    }} // Driver Codepublic 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.
    <script>    // Javascript program for the above approach // Function to reconstruct the array// arr[] with xor of adjacent elementsfunction game_with_number(arr,n){    // Iterate through each element    for (let 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 arrayfunction print(arr,n){    for (let i = 0; i < n; i++) {        document.write(arr[i]+" ");    }} // Driver Code     //Inputs    let arr = [10, 11, 1, 2, 3 ];     // Length of the array given    let n = arr.length;     // Function call to reconstruct the arr[]    let new_arr = game_with_number(arr, n);     // Function call to print arr[]    print(new_arr, n); // This code is contributed by// Potta Lokesh         </script>
1 10 3 1 3 
Time Complexity: O(N)Auxiliary Space: O(1)
 
mohit kumar 29
lokeshpotta20
subhammahato348
target_2
akshaysingh98088
khushboogoyal499
Bitwise-XOR
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Find duplicates in O(n) time and O(1) extra space | Set 1
Trapping Rain Water
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Coin Change | DP-7