Find the XOR of the elements in the given range [L, R] with the value K for a given set of queries
Last Updated :
11 Nov, 2021
Given an array arr[] and Q queries, the task is to find the resulting updated array after Q queries. There are two types of queries and the following operation is performed by them:
- Update(L, R, K): Perform XOR for each element within the range L to R with K.
- Display(): Display the current state of the given array.
Examples:
Input: arr[] = {1, 2, 3, 4, 5, 6}, Q = 2
Query 1: Update(1, 5, 3)
Query 2: Display()
Output: 2 1 0 7 6 6
Explanation:
The update query performs XOR of all elements in the range [1, 5].
After performing the update query, the above array is displayed for display query because:
1 ^ 3 = 2
2 ^ 3 = 1
3 ^ 3 = 0
4 ^ 3 = 7
5 ^ 3 = 6
Input: arr[] = {2, 4, 6, 8, 10}, Q = 3
Query 1: Update(1, 3, 2)
Query 2: Update(2, 4, 3)
Query 3: Display()
Output: 0 5 7 11 10
Explanation:
There are two update queries.
After performing the first update query, the given array is changed to {0, 6, 4, 8, 10}. This is because:
2 ^ 2 = 0
4 ^ 2 = 6
6 ^ 2 = 4
After obtaining this array, this array further gets changed for the second query as the {0, 5, 7, 11, 10}. This is because:
6 ^ 3 = 5
4 ^ 3 = 7
8 ^ 3 = 11
Naive Approach: The naive approach for this problem is for every update query, run a loop from L to R and perform the XOR operation with the given K with all the elements from arr[L] to arr[R]. In order to perform the display query, simply print the array arr[]. The time complexity for this approach is O(NQ) where N is the length of the array and Q is the number of queries.
Efficient Approach: The idea is to use a kadane’s algorithm for this problem.
- An empty array res[] is defined with the same length as the original array.
- For every update query {L, R, K}, the res[] array is modified as:
- XOR K to res[L]
- XOR K to res[R + 1]
- Whenever the user gives a display query:
- Initialize a counter variable ‘i’ to the index 1.
- Perform the operation:
res[i] = res[i] ^ res[i - 1]
- Initialize a counter variable ‘i’ to the index 0.
- For every index in the array’s the required answer is:
arr[i] ^ res[i]
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void update( int res[], int L, int R, int K)
{
L -= 1;
R -= 1;
res[L] ^= K;
res[R + 1] ^= K;
}
void display( int arr[], int res[], int n)
{
for ( int i = 1; i < n; i++) {
res[i] = res[i] ^ res[i - 1];
}
for ( int i = 0; i < n; i++) {
cout << (arr[i] ^ res[i]) << " " ;
}
cout << endl;
}
int main()
{
int arr[] = { 2, 4, 6, 8, 10 };
int N = sizeof (arr) / sizeof (arr[0]);
int res[N];
memset (res, 0, sizeof (res));
int L = 1, R = 3, K = 2;
update(res, L, R, K);
L = 2;
R = 4;
K = 3;
update(res, L, R, K);
display(arr, res, N);
return 0;
}
|
Java
class GFG{
static void update( int res[], int L,
int R, int K)
{
L -= 1 ;
R -= 1 ;
res[L] ^= K;
res[R + 1 ] ^= K;
}
static void display( int arr[],
int res[], int n)
{
int i;
for (i = 1 ; i < n; i++)
{
res[i] = res[i] ^ res[i - 1 ];
}
for (i = 0 ; i < n; i++)
{
System.out.print((arr[i] ^ res[i]) + " " );
}
System.out.println();
}
public static void main(String []args)
{
int arr[] = { 2 , 4 , 6 , 8 , 10 };
int N = arr.length;
int res[] = new int [N];
int L = 1 , R = 3 , K = 2 ;
update(res, L, R, K);
L = 2 ;
R = 4 ;
K = 3 ;
update(res, L, R, K);
display(arr, res, N);
}
}
|
Python3
def update(res, L, R, K):
L = L - 1
R = R - 1
res[L] = res[L] ^ K
res[R + 1 ] = res[R + 1 ] ^ K
def display(arr, res, n):
for i in range ( 1 , n):
res[i] = res[i] ^ res[i - 1 ]
for i in range ( 0 , n):
print (arr[i] ^ res[i], end = " " )
arr = [ 2 , 4 , 6 , 8 , 10 ]
N = len (arr)
res = [ 0 ] * N
L = 1
R = 3
K = 2
update(res, L, R, K)
L = 2
R = 4
K = 3
update(res, L, R, K)
display(arr, res, N)
|
C#
using System;
class GFG{
static void update( int []res, int L,
int R, int K)
{
L -= 1;
R -= 1;
res[L] ^= K;
res[R + 1] ^= K;
}
static void display( int []arr,
int []res, int n)
{
int i;
for (i = 1; i < n; i++)
{
res[i] = res[i] ^ res[i - 1];
}
for (i = 0; i < n; i++)
{
Console.Write((arr[i] ^ res[i]) + " " );
}
Console.WriteLine();
}
public static void Main()
{
int []arr = { 2, 4, 6, 8, 10 };
int N = arr.Length;
int []res = new int [N];
int L = 1, R = 3, K = 2;
update(res, L, R, K);
L = 2;
R = 4;
K = 3;
update(res, L, R, K);
display(arr, res, N);
}
}
|
Javascript
<script>
function update(res, L, R, K)
{
L -= 1;
R -= 1;
res[L] ^= K;
res[R + 1] ^= K;
}
function display(arr, res, n)
{
let i;
for (i = 1; i < n; i++)
{
res[i] = res[i] ^ res[i - 1];
}
for (i = 0; i < n; i++)
{
document.write((arr[i] ^ res[i]) + " " );
}
document.write( "<br/>" );
}
let arr = [ 2, 4, 6, 8, 10 ];
let N = arr.length;
let res = Array.from({length: N}, (_, i) => 0);
let L = 1, R = 3, K = 2;
update(res, L, R, K);
L = 2;
R = 4;
K = 3;
update(res, L, R, K);
display(arr, res, N);
</script>
|
Time Complexity:
- The time complexity for the update is O(1).
- The time complexity for displaying the array is O(N).
Auxiliary Space: O(N)
Note: This approach works very well when the update queries are very high compared to the display queries.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...