

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find elements of array using XOR of consecutive elements in C++
Consider we have to find a list of n elements. But we have the XOR value of two consecutive elements of the actual array. Also the first element of the actual is given. So if the array elements are a, b, c, d, e, f, then the given array will be a^b, b^c, c^d, d^e and e^f.
As the first number is given, named a, that can help us to find all numbers. If we want to find the second element of the actual array, then we have to perform b = a ^ arr[i], for second element c = b ^ arr[1] and so on.
Example
#include<iostream> using namespace std; void findActualElements(int a, int arr[], int n) { int actual[n + 1]; actual[0] = a; for (int i = 0; i < n; i++) { actual[i + 1] = arr[i] ^ actual[i]; } for (int i = 0; i < n + 1; i++) cout << actual[i] << " "; } int main() { int arr[] = { 12, 5, 26, 7 }; int n = sizeof(arr) / sizeof(arr[0]); int a = 6; findActualElements(a, arr, n); }
Output
6 10 15 21 18
- Related Questions & Answers
- Maximum sum of n consecutive elements of array in JavaScript
- Find consecutive elements average JavaScript
- Consecutive elements sum array in JavaScript
- Absolute Difference of all pairwise consecutive elements in an array (C++)?
- Construct an array from GCDs of consecutive elements in given array in C++
- Python – Summation of consecutive elements power
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- Compress array to group consecutive elements JavaScript
- XOR of all elements of array with set bits equal to K in C++
- Check if array elements are consecutive in Python
- Count of only repeated element in a sorted array of consecutive elements in C++
- Compute the differences between consecutive elements of a masked array in Numpy
- Find original array from encrypted array (An array of sums of other elements) using C++.
- Rearrange an array to minimize sum of product of consecutive pair elements in C++
- Reset keys of array elements using PHP ?
Advertisements