- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Maximum XOR value of a sub-array of size k in C++
In this problem, we are given an array arr[] consisting of n elements and an integer k. Our task is to find the Maximum XOR value of a sub-array of size k.
Let’s take an example to understand the problem,
Input
arr[] = {3, 1, 6, 2 ,7, 9} k = 3
Output
12
Explanation
All subarray and the xor of all element of size k,
{3, 1, 6} = 4 {1, 6, 2} = 5 {6, 2, 7} = 3 {2, 7, 9} = 12
Solution Approach
A simple solution to the problem is by using two loops. One to iterate over the array and other to find the XOR of all elements of the subarray. And then return the maximum of all.
This solution is ok but a better approach to solve the problem can be made. We need to find the sum by starting from the subarray of size k starting from
index 0. And then iterating the array and adding a new element to XOR and deleting the first one. Deletion is possible by using the formula x^a^x = a.
So, for each interaction we will first perform, XOR ^ arr[i - k], which will give the value of the subarray from last index + 1 to the current index - 1.
Then we will perform XOR of subarray with arr[i], to get the current XOR. We will find and return the maximum value out of all XOR’s.
Program to illustrate the working of our solution,
Example
#include<iostream> using namespace std; int findMaxSubArrayXOR(int arr[] , int n , int k) { int currentXORVal = 0 ; for (int i = 0 ; i < k ; i++) currentXORVal = currentXORVal ^ arr[i]; int maxXor = currentXORVal; for (int i = k ; i < n; i++) { currentXORVal = currentXORVal ^ arr[i-k]; currentXORVal = currentXORVal ^ arr[i]; maxXor = max(maxXor, currentXORVal); } return maxXor; } int main() { int arr[] = {3, 1, 6, 2, 7, 9}; int n = sizeof(arr)/sizeof(arr[0]); int k = 3; cout<<"The maximum XOR of subarray of size "<<k<<" is "<<findMaxSubArrayXOR(arr, n, k); return 0; }
Output
The maximum XOR of subarray of size 3 is 12
- Related Articles
- Maximum product quadruple (sub-sequence of size 4) in array in C++
- Maximum size of sub-array that satisfies the given condition in C++
- Maximum value of XOR among all triplets of an array in C++
- Maximum size of sub-array that satisfies the given condition in C++ program
- Find maximum (or minimum) sum of a subarray of size k in C++
- Maximize the size of array by deleting exactly k sub-arrays to make array prime in C++
- Maximum XOR value of a pair from a range in C++
- Maximum product of subsequence of size k in C++
- Maximum XOR of Two Numbers in an Array in C++
- Maximum XOR value in matrix in C++
- Find a value whose XOR with given number is maximum in C++
- Maximum length of a sub-array with ugly numbers in C++
- Print maximum sum square sub-matrix of given size in C Program.
- Find k maximum elements of array in original order in C++
- Bitwise AND of sub-array closest to K in C++
