Queries to update each element in subarray to Bitwise XOR with a given value
Last Updated :
25 Apr, 2023
Given an array arr[], and queries Q[][] of the form (l, r, val), the task for each query is to update all the elements in the indices [l – 1, r – 1] to Bitwise XOR with val. Print the final array obtained after completing all queries.
Examples:
Input: arr[] = {2, 3, 6, 5, 4}, Q[][] = {{1, 3, 2}, {2, 4, 4}}
Output: 0 5 0 1 4
Explanation:
1st Query: Concerned subarray {2, 3, 6} modifies to {0, 1, 4} after replacing each element with its XOR with val(= 2)
The modified array is {0, 1, 4, 5, 4}
2nd Query: Concerned subarray {1, 4, 5} modifies to {5, 0, 1} after replacing each element with its XOR with val(= 4)
Hence, the final array is {0, 5, 0, 1, 4}
Input: arr[] = {1, 3, 5}, Q[][] = {{1, 2, 8}, {2, 3, 3}}
Output: 9 8 6
Naive Approach:
The simplest approach to solve this problem is to traverse indices [l – 1, r – 1] for each query and replace arr[i] by arr[i]^val. After completing all queries, print the modified array.
Time Complexity: O(N*sizeof(Q))
Auxiliary Space: O(1)
Approach: Follow the steps to solve the problem by processing each query in constant time complexity:
- Initialize an array temp[] with all zeroes.
- For each query of the form (l, r, val), update temp[l – 1] and temp[r] with their respective XOR with val.
- After completing the above step for each query, convert temp[] to a prefix XOR array.
- Finally, perform update arr[] by replacing every ith element with its XOR.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findxor( int temp[], int N,
int lo, int hi, int val)
{
temp[lo] ^= val;
if (hi != N - 1)
temp[hi + 1] ^= val;
}
void updateArray( int temp[], int N)
{
for ( int i = 1; i < N; i++)
temp[i] ^= temp[i - 1];
}
void xorQuery( int arr[], int n,
vector<vector< int > > Q)
{
int temp[n];
memset (temp, 0, sizeof (temp));
for ( int i = 0; i < Q.size(); i++) {
findxor(temp, n, Q[i][0] - 1,
Q[i][1] - 1, Q[i][2]);
}
updateArray(temp, n);
for ( int i = 0; i < n; ++i) {
cout << (arr[i] ^ temp[i]) << " " ;
}
}
int main()
{
int arr[] = { 2, 3, 6, 5, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
vector<vector< int > > Q = { { 1, 3, 2 },
{ 2, 4, 4 } };
xorQuery(arr, n, Q);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void findxor( int temp[], int N,
int lo, int hi, int val)
{
temp[lo] ^= val;
if (hi != N - 1 )
temp[hi + 1 ] ^= val;
}
static void updateArray( int temp[], int N)
{
for ( int i = 1 ; i < N; i++)
temp[i] ^= temp[i - 1 ];
}
static void xorQuery( int arr[], int n,
int [][] Q)
{
int [] temp = new int [n];
for ( int i = 0 ; i < Q.length; i++)
{
findxor(temp, n, Q[i][ 0 ] - 1 ,
Q[i][ 1 ] - 1 ,
Q[i][ 2 ]);
}
updateArray(temp, n);
for ( int i = 0 ; i < n; ++i)
{
System.out.print((arr[i] ^ temp[i]) + " " );
}
}
public static void main (String[] args)
{
int arr[] = { 2 , 3 , 6 , 5 , 4 };
int n = arr.length;
int [][] Q = { { 1 , 3 , 2 },
{ 2 , 4 , 4 } };
xorQuery(arr, n, Q);
}
}
|
Python3
def findxor(temp, N, lo, hi, val):
temp[lo] ^ = val
if (hi ! = N - 1 ):
temp[hi + 1 ] ^ = val
def updateArray(temp, N):
for i in range ( 1 , N):
temp[i] ^ = temp[i - 1 ]
def xorQuery(arr, n, Q):
temp = [ 0 ] * n
for i in range ( len (Q)):
findxor(temp, n, Q[i][ 0 ] - 1 ,
Q[i][ 1 ] - 1 ,
Q[i][ 2 ])
updateArray(temp, n)
for i in range (n):
print ((arr[i] ^ temp[i]), end = " " )
if __name__ = = "__main__" :
arr = [ 2 , 3 , 6 , 5 , 4 ]
n = len (arr)
Q = [ [ 1 , 3 , 2 ],
[ 2 , 4 , 4 ] ]
xorQuery(arr, n, Q)
|
C#
using System;
class GFG{
static void findxor( int []temp, int N,
int lo, int hi, int val)
{
temp[lo] ^= val;
if (hi != N - 1)
temp[hi + 1] ^= val;
}
static void updateArray( int []temp, int N)
{
for ( int i = 1; i < N; i++)
temp[i] ^= temp[i - 1];
}
static void xorQuery( int []arr, int n,
int [,] Q)
{
int [] temp = new int [n];
for ( int i = 0; i < Q.GetLength(0); i++)
{
findxor(temp, n, Q[i, 0] - 1,
Q[i, 1] - 1,
Q[i, 2]);
}
updateArray(temp, n);
for ( int i = 0; i < n; ++i)
{
Console.Write((arr[i] ^ temp[i]) + " " );
}
}
public static void Main(String[] args)
{
int []arr = { 2, 3, 6, 5, 4 };
int n = arr.Length;
int [,] Q = { { 1, 3, 2 },
{ 2, 4, 4 } };
xorQuery(arr, n, Q);
}
}
|
Javascript
<script>
function findxor(temp, N, lo, hi, val) {
temp[lo] ^= val;
if (hi != N - 1)
temp[hi + 1] ^= val;
}
function updateArray(temp, N) {
for (let i = 1; i < N; i++)
temp[i] ^= temp[i - 1];
}
function xorQuery(arr, n, Q) {
let temp = new Array(n);
temp.fill(0)
for (let i = 0; i < Q.length; i++) {
findxor(temp, n, Q[i][0] - 1,
Q[i][1] - 1, Q[i][2]);
}
updateArray(temp, n);
for (let i = 0; i < n; ++i) {
document.write(`${arr[i] ^ temp[i]} `);
}
}
let arr = [2, 3, 6, 5, 4];
let n = arr.length;
let Q = [[1, 3, 2],
[2, 4, 4]];
xorQuery(arr, n, Q);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...