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:
- 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++
// C++ implementation
// of the above approach
#include <iostream>
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
<script>
// Javascript program for the above approach
// Function to reconstruct the array
// arr[] with xor of adjacent elements
function 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 array
function 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>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Kth array element after M replacements of array elements by XOR of adjacent pairs Given an array arr[] of size N and two integers M and K, the task is to find the array element at the Kth index after performing following M operations on the given array. In a single operation, a new array is formed whose elements have the Bitwise XOR values of the adjacent elements of the current
14 min read
Make Array elements equal by replacing adjacent elements with their XOR Given an array A[] consisting of N integers, the task is to check if it is possible to reduce array of at least length 2 such that all the elements in the array are equal. In an operation, choose any index i, and replace A[i] and A[i+1] with their XOR value. Example: Input: A[] = {0, 2, 2}Output: YE
14 min read
Generate an array from given pairs of adjacent elements Given a 2D array arr[][] consisting of N pairs of integers such that the two elements in each row indicates that they are adjacent elements in the original array. The task is to construct an array with given pairs of adjacent elements of arr[]. Examples Input: arr[] = {{5, 1 }, {3, 4 }, {3, 5}} Outp
8 min read
Construct an array from XOR of all elements of array except element at same index Given an array A[] having n positive elements. The task to create another array B[] such as B[i] is XOR of all elements of array A[] except A[i].Examples : Input : A[] = {2, 1, 5, 9} Output : B[] = {13, 14, 10, 6} Input : A[] = {2, 1, 3, 6} Output : B[] = {4, 7, 5, 0} Recommended PracticeXOR of all
5 min read
Find the original Array using XOR values of all adjacent elements Given a sequence arr[] of N-1 elements which is xor of all adjacent pairs in an array, the task is to find that original array from the arr[].Note: It is given that the N is always odd and arr[] contains the permutation of N natural number.Examples: Input: arr[] = {3, 1} Output: 1 2 3 Explanation: T
9 min read