Given an array arr[] of length N, , a matrix of dimensions N * N was defined on the array arr[] where Mi, j = arri & arrj. Given four integers X, Y, S and T, the task is to find the Bitwise XOR of all the elements of the submatrix from top-left (X, Y) to bottom-right (S, T).
Examples:
Input: N = 3, A[] = {2, 3, 4}, (X, Y)=(0, 1), (S, T)=(2, 2)
Output: 5
Explanation:
Matrix defined on A is
{{(2&2), (2&3), (2&4)},
{(3&2), (3&3), (3&4)},
{(4&2), (4&3), (4&4)}}
Finally, the matrix will be:
{{2, 2, 0},
{2, 3, 0},
{0, 0, 4}}
XOR value= (2^0)^(3^0)^(0^4) = 5
Input: N=3, A[]={1, 2, 3}, (X, Y)=(0, 1), (S, T)=(1, 2)
Output: 1
Naive approach: The simplest approach is to generate the matrix M from the given array and calculate the Bitwise XOR of all the elements present in the given submatrix of M.
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: The idea is to use the following distributive property of the ‘XOR’ and ‘AND’ operations:
(A & B) ^ (A & C) = A & (B ^ C)
Therefore, the final XOR of the sub-matrix from top-left (X, Y) to bottom-right (S, T) can be calculated from the following equation:
Final XOR
= (XOR of row X)^(XOR of row X+1)^. . . . ^(XOR of row S)
= (AX & (AY ^. . . .^ AT)) ^ ….
. . . ^(AS & (AY^. . . .^AT))
= (AY^. . . .^AT)&(AX^. . .^AS)
- Iterate over the array from indices Y to T and compute the XOR of the elements.
- Traverse the array from indices X to S and compute the XOR of the elements.
- Finally, compute the Bitwise AND of computed XOR’s, which is equal to the Bitwise XOR of the submatrix from (X, Y) to (S, T)
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int submatrix_xor( int * A, int N,
int X, int Y,
int S, int T)
{
int left_xor = 0, i, right_xor = 0;
for (i = Y; i <= T; i++) {
left_xor ^= A[i];
}
for (i = X; i <= S; i++) {
right_xor ^= A[i];
}
return left_xor & right_xor;
}
int main()
{
int A[3] = { 2, 3, 4 }, X = 0,
Y = 1, S = 2, T = 2, N = 3;
cout << submatrix_xor(A, N, X,
Y, S, T);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int submatrix_xor( int [] A, int N,
int X, int Y,
int S, int T)
{
int left_xor = 0 , i, right_xor = 0 ;
for (i = Y; i <= T; i++)
{
left_xor ^= A[i];
}
for (i = X; i <= S; i++)
{
right_xor ^= A[i];
}
return left_xor & right_xor;
}
public static void main (String[] args)
{
int [] A = { 2 , 3 , 4 };
int X = 0 , Y = 1 , S = 2 ,
T = 2 , N = 3 ;
System.out.print(submatrix_xor(A, N, X,
Y, S, T));
}
}
|
Python3
def submatrix_xor(A, N, X, Y, S, T):
left_xor = 0
i = 0
right_xor = 0
for i in range (Y, T + 1 ):
left_xor ^ = A[i]
for i in range (X, S + 1 ):
right_xor ^ = A[i]
return left_xor & right_xor
if __name__ = = '__main__' :
A = [ 2 , 3 , 4 ]
X = 0
Y = 1
S = 2
T = 2
N = 3
print (submatrix_xor(A, N, X, Y, S, T))
|
C#
using System;
class GFG{
static int submatrix_xor( int [] A, int N,
int X, int Y,
int S, int T)
{
int left_xor = 0, i, right_xor = 0;
for (i = Y; i <= T; i++)
{
left_xor ^= A[i];
}
for (i = X; i <= S; i++)
{
right_xor ^= A[i];
}
return left_xor & right_xor;
}
public static void Main ()
{
int [] A = { 2, 3, 4 };
int X = 0, Y = 1, S = 2,
T = 2, N = 3;
Console.Write(submatrix_xor(A, N, X,
Y, S, T));
}
}
|
Javascript
<script>
function submatrix_xor(A, N, X, Y, S, T)
{
let left_xor = 0, i, right_xor = 0;
for (i = Y; i <= T; i++)
{
left_xor ^= A[i];
}
for (i = X; i <= S; i++)
{
right_xor ^= A[i];
}
return left_xor & right_xor;
}
let A = [ 2, 3, 4 ];
let X = 0, Y = 1, S = 2,
T = 2, N = 3;
document.write(submatrix_xor(A, N, X,
Y, S, T));
</script>
|
Time Complexity: O(N), where N is the size of the array
Auxiliary Space: O(1)