XOR of XORs of all sub-matrices
Given a ‘N*N’ matrix, the task is to find the XOR of XORs of all possible sub-matrices.
Examples:
Input :arr = {{3, 1},
{1, 3}}
Output : 0
Explanation: All the elements lie in 4 submatrices each.
4 being even, there total contribution towards
final answer becomes 0. Thus, ans = 0.
Input : arr = {{6, 7, 13},
{8, 3, 4},
{9, 7, 6}};
Output : 4
A Simple Approach is to generate all the possible submatrices, find the XOR of each submatrix uniquely, and then XOR them all up. The time complexity of this approach will be O(n6).
Better solution: For each index(R, C), we will try to find the number of sub-matrices in which that index lies. If the number of sub-matrices is odd, then the final answer will be updated as ans = (ans ^ arr[R][C]). In case of even, we don’t need to update the answer. This works because a number XORed with itself gives zero and the order of operation doesn’t affect the final XOR value.
Assuming 0-based indexing, the number of sub-matrices an index (R, C) lies in equals
(R + 1)*(C + 1)*(N - R)*(N - C)
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
#define n 3
int submatrixXor( int arr[][n])
{
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
int top_left = (i + 1) * (j + 1);
int bottom_right = (n - i) * (n - j);
if ((top_left % 2 == 1) && (bottom_right % 2 == 1))
ans = (ans ^ arr[i][j]);
}
}
return ans;
}
int main()
{
int arr[][n] = { { 6, 7, 13 },
{ 8, 3, 4 },
{ 9, 7, 6 } };
cout << submatrixXor(arr);
return 0;
}
|
Java
class GFG
{
static int submatrixXor( int [][]arr)
{
int n = 3 ;
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
int top_left = (i + 1 ) * (j + 1 );
int bottom_right = (n - i) * (n - j);
if ((top_left % 2 == 1 ) &&
(bottom_right % 2 == 1 ))
ans = (ans ^ arr[i][j]);
}
}
return ans;
}
public static void main(String[] args)
{
int [][] arr = {{ 6 , 7 , 13 },
{ 8 , 3 , 4 },
{ 9 , 7 , 6 }};
System.out.println(submatrixXor(arr));
}
}
|
Python3
def submatrixXor(arr, n):
ans = 0
for i in range ( 0 , n):
for j in range ( 0 , n):
top_left = (i + 1 ) * (j + 1 )
bottom_right = (n - i) * (n - j)
if (top_left % 2 = = 1 and
bottom_right % 2 = = 1 ):
ans = (ans ^ arr[i][j])
return ans
n = 3
arr = [[ 6 , 7 , 13 ],
[ 8 , 3 , 4 ],
[ 9 , 7 , 6 ]]
print (submatrixXor(arr, n))
|
C#
using System;
class GFG
{
static int submatrixXor( int [,]arr)
{
int n = 3;
int ans = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
int top_left = (i + 1) * (j + 1);
int bottom_right = (n - i) * (n - j);
if ((top_left % 2 == 1) &&
(bottom_right % 2 == 1))
ans = (ans ^ arr[i, j]);
}
}
return ans;
}
public static void Main()
{
int [, ]arr = {{ 6, 7, 13},
{ 8, 3, 4 },
{ 9, 7, 6 }};
Console.Write(submatrixXor(arr));
}
}
|
PHP
<?php
function submatrixXor( $arr )
{
$ans = 0;
$n = 3 ;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
$top_left = ( $i + 1) * ( $j + 1);
$bottom_right = ( $n - $i ) * ( $n - $j );
if (( $top_left % 2 == 1) &&
( $bottom_right % 2 == 1))
$ans = ( $ans ^ $arr [ $i ][ $j ]);
}
}
return $ans ;
}
$arr = array ( array ( 6, 7, 13 ),
array ( 8, 3, 4 ),
array ( 9, 7, 6 ));
echo submatrixXor( $arr );
# This code is contributed by Ryuga
?>
|
Javascript
<script>
const n = 3;
function submatrixXor(arr)
{
let ans = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
let top_left = (i + 1) * (j + 1);
let bottom_right = (n - i) * (n - j);
if ((top_left % 2 == 1) &&
(bottom_right % 2 == 1))
ans = (ans ^ arr[i][j]);
}
}
return ans;
}
let arr = [ [ 6, 7, 13 ],
[ 8, 3, 4 ],
[ 9, 7, 6 ] ];
document.write(submatrixXor(arr));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...