Find elements of array using XOR of consecutive elements
Last Updated :
26 Feb, 2023
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 n 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 a (first elements), and to find the next element i.e b we have to xor a by arr[0], similarly for c 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++
// C++ program to find the array elements
// using XOR of consecutive elements
#include <bits/stdc++.h>
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 original
// elements
int elements[n + 1];
// first element a i.e elements[0]=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++)
cout << elements[i] << " ";
}
// Driver Code
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
// 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 original
// elements
int elements[] = new int[n + 1];
// first element a i.e elements[0]=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[0]=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 original
// elements
int []elements = new int[n + 1];
// first element a i.e elements[0]=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++)
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
<?php
// PHP program to find the array elements
// using XOR of consecutive elements
// Function to find the array elements
// using XOR of consecutive elements
function getElements($a, &$arr, &$n)
{
// array to store the original
// elements
// first element a i.e elements[0]=a
$elements[0] = $a;
for ($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 ($i = 0; $i < $n + 1; $i++)
{
echo($elements[$i] . " ");
}
}
// Driver Code
$arr = array(13, 2, 6, 1);
$n = sizeof($arr);
$a = 5;
getElements($a, $arr, $n);
// This code is contributed by Shivi_Aggarwal
?>
JavaScript
<script>
// Javascript program to find the array elements
// using XOR of consecutive elements
// Function to find the array elements
// using XOR of consecutive elements
function getElements(a, arr, n)
{
// Array to store the original
// elements
let elements = new Array(n + 1);
for(let i = 0; i < n + 1; i++)
{
elements[i] = 0;
}
// first element a i.e elements[0]=a
elements[0] = a;
for(let 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(let i = 0; i < n + 1; i++)
document.write( elements[i] + " ");
}
// Driver Code
let arr = [ 13, 2, 6, 1 ];
let n = arr.length;
let a = 5;
getElements(a, arr, n);
// This code is contributed by unknown2108
</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.
Similar Reads
Replacing an element makes array elements consecutive
Given an array of positive distinct integers. We need to find the only element whose replacement with any other value makes array elements distinct consecutive. If it is not possible to make array elements consecutive, return -1. Examples : Input : arr[] = {45, 42, 46, 48, 47} Output : 42 Explanatio
8 min read
Find array using different XORs of elements in groups of size 4
Given an array q[] of XOR queries of size N (N is a multiple of 4) which describe an array of the same size as follows: q[0 - 3] describe arr[0 - 3], q[4 - 7] describe arr[4 - 7], and so on... If arr[0 - 3] = {a1, a2, a3, a4} then q[0 - 3] = {a1 ? a2 ? a3, a1 ? a2 ? a4, a1 ? a3 ? a4, a2 ? a3 ? a4} T
7 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 missing element in a sorted array of consecutive numbers
Given an array arr[] of n distinct integers. Elements are placed sequentially in ascending order with one element missing. The task is to find the missing element.Examples: Input: arr[] = {1, 2, 4, 5, 6, 7, 8, 9} Output: 3Input: arr[] = {-4, -3, -1, 0, 1, 2} Output: -2Input: arr[] = {1, 2, 3, 4} Out
7 min read
Modulus of all pairwise consecutive elements in an Array
Given an array of N elements. The task is to print the modulus of all of the pairwise consecutive elements. That is for all pair of consecutive elements say ((a[i], a[i+1])), print (a[i] % a[i+1]). Note: Consecutive pairs of an array of size N are (a[i], a[i+1]) for all i ranging from 0 to N-2. Exam
4 min read
Check if Sum and XOR of all elements of array is equal
Given an array arr[], the task is to check if sum of all elements of an array is equal to XOR of all elements of array. Example: Input: arr[] = [1, 2] Output: YES Explanation: Sum = (1+2) = 3 XOR = (1^2) = 3 Input: arr[] = [6, 3, 7, 10] Output: NO Explanation: Sum = (6 + 3 + 7 + 10) = 26 XOR = (6 ^
4 min read
Minimum increments to convert to an array of consecutive integers
Given an array arr[] with N elements, the task is to find the minimum number of operations required so that an Arithmetic Progression with the array elements is achieved with common difference as 1. In a single operation, any element can be incremented by 1.Examples: Input: arr[] = {4, 4, 5, 5, 7} O
9 min read
Sum of Bitwise XOR of each array element with all other array elements
Given an array arr[] of length N, the task for every array element is to print the sum of its Bitwise XOR with all other array elements. Examples: Input: arr[] = {1, 2, 3}Output: 5 4 3Explanation:For arr[0]: arr[0] ^ arr[0] + arr[0] ^ arr[1] + arr[0] ^ arr[2] = 1^1 + 1^2 + 1^3 = 0 + 3 + 2 = 5For arr
9 min read
Sum of Bitwise XOR of elements of an array with all elements of another array
Given an array arr[] of size N and an array Q[], the task is to calculate the sum of Bitwise XOR of all elements of the array arr[] with each element of the array q[]. Examples: Input: arr[ ] = {5, 2, 3}, Q[ ] = {3, 8, 7}Output: 7 34 11Explanation:For Q[0] ( = 3): Sum = 5 ^ 3 + 2 ^ 3 + 3 ^ 3 = 7.For
9 min read
Find array whose elements are XOR of adjacent elements in given array
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, a
5 min read