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 us 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
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[0], similarly for xor arr[1] 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[0] contains a^b. Therefore,
a ^ arr[0] = a ^ a ^ b
= 0 ^ b
= b
Similarly, arr[i] contains XOR of ai and ai+1. Therefore,
ai ^ arr[i] = ai ^ ai ^ ai+1
= 0 ^ ai+1
= ai+1
Algorithm:
Step 1: Start
Step 2: Let’s start with creating a function named getElements with three parameters: an integer a, an array arr of size n, and an integer n.
Step 3: Now let’s form a new integer array of size n+1 in which we will store the value of the original array.
Step 4: Now set the value of the 0th index to a.
Step 5: Start a for loop which traverses through the given array from index 0 to n-1.
Step 6: Calculate the XOR of the current element of arr with the previous element of elements for each iteration, then store the result in the subsequent index of elements.
Step 7: Now print all elements with another for a loop.
Step 8: End
Below is the implementation of the above approach
C++
#include <bits/stdc++.h>
using namespace std;
void getElements( int a, int arr[], int n)
{
int elements[n + 1];
elements[0] = a;
for ( int i = 0; i < n; i++) {
elements[i + 1] = arr[i] ^ elements[i];
}
for ( int i = 0; i < n + 1; i++)
cout << elements[i] << " " ;
}
int main()
{
int arr[] = { 13, 2, 6, 1 };
int n = sizeof (arr) / sizeof (arr[0]);
int a = 5;
getElements(a, arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void getElements( int a, int arr[], int n)
{
int elements[] = new int [n + 1 ];
elements[ 0 ] = a;
for ( int i = 0 ; i < n; i++) {
elements[i + 1 ] = arr[i] ^ elements[i];
}
for ( int i = 0 ; i < n + 1 ; i++)
System.out.print( elements[i] + " " );
}
public static void main (String[] args) {
int arr[] = { 13 , 2 , 6 , 1 };
int n = arr.length;
int a = 5 ;
getElements(a, arr, n);
}
}
|
Python3
def getElements(a, arr, n):
elements = [ 1 for i in range (n + 1 )]
elements[ 0 ] = a
for i in range (n):
elements[i + 1 ] = arr[i] ^ elements[i]
for i in range (n + 1 ):
print (elements[i], end = " " )
arr = [ 13 , 2 , 6 , 1 ]
n = len (arr)
a = 5
getElements(a, arr, n)
|
C#
using System;
class GFG {
static void getElements( int a, int []arr, int n)
{
int []elements = new int [n + 1];
elements[0] = a;
for ( int i = 0; i < n; i++) {
elements[i + 1] = arr[i] ^ elements[i];
}
for ( int i = 0; i < n + 1; i++)
Console.Write( elements[i] + " " );
}
public static void Main () {
int []arr = { 13, 2, 6, 1 };
int n = arr.Length;
int a = 5;
getElements(a, arr, n);
}
}
|
PHP
<?php
function getElements( $a , & $arr , & $n )
{
$elements [0] = $a ;
for ( $i = 0; $i < $n ; $i ++)
{
$elements [ $i + 1] = $arr [ $i ] ^ $elements [ $i ];
}
for ( $i = 0; $i < $n + 1; $i ++)
{
echo ( $elements [ $i ] . " " );
}
}
$arr = array (13, 2, 6, 1);
$n = sizeof( $arr );
$a = 5;
getElements( $a , $arr , $n );
?>
|
Javascript
<script>
function getElements(a, arr, n)
{
let elements = new Array(n + 1);
for (let i = 0; i < n + 1; i++)
{
elements[i] = 0;
}
elements[0] = a;
for (let i = 0; i < n; i++)
{
elements[i + 1] = arr[i] ^ elements[i];
}
for (let i = 0; i < n + 1; i++)
document.write( elements[i] + " " );
}
let arr = [ 13, 2, 6, 1 ];
let n = arr.length;
let a = 5;
getElements(a, arr, n);
</script>
|
Complexity Analysis:
- Time Complexity: O(N), since there runs a loop for N times.
- Auxiliary Space: O(N), since N extra space has been taken.
Last Updated :
26 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...