Maximum XOR value in matrix
Last Updated :
12 Dec, 2022
Given a square matrix (N X N), the task is to find the maximum XOR value of a complete row or a complete column.
Examples :
Input : N = 3
mat[3][3] = {{1, 0, 4},
{3, 7, 2},
{5, 9, 10} };
Output : 14
We get this maximum XOR value by doing XOR
of elements in second column 0 ^ 7 ^ 9 = 14
Input : N = 4
mat[4][4] = { {1, 2, 3, 6},
{4, 5, 6,7},
{7, 8, 9, 10},
{2, 4, 5, 11}}
Output : 12
A simple solution of this problem is we can traverse the matrix twice and calculate maximum xor value row-wise & column wise ,and at last return the maximum between (xor_row , xor_column).
A efficient solution is we can traverse matrix only one time and calculate max XOR value .
- Start traverse the matrix and calculate XOR at each index row and column wise. We can compute both values by using indexes in reverse way. This is possible because matrix is a square matrix.
- Store the maximum of both.
Below is the implementation :
C++
#include<iostream>
using namespace std;
const int MAX = 1000;
int maxXOR( int mat[][MAX], int N)
{
int r_xor, c_xor;
int max_xor = 0;
for ( int i = 0 ; i < N ; i++)
{
r_xor = 0, c_xor = 0;
for ( int j = 0 ; j < N ; j++)
{
r_xor = r_xor^mat[i][j];
c_xor = c_xor^mat[j][i];
}
if (max_xor < max(r_xor, c_xor))
max_xor = max(r_xor, c_xor);
}
return max_xor;
}
int main()
{
int N = 3;
int mat[][MAX] = {{1 , 5, 4},
{3 , 7, 2 },
{5 , 9, 10}
};
cout << "maximum XOR value : "
<< maxXOR(mat, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
static final int MAX = 1000 ;
static int maxXOR( int mat[][], int N)
{
int r_xor, c_xor;
int max_xor = 0 ;
for ( int i = 0 ; i < N ; i++)
{
r_xor = 0 ; c_xor = 0 ;
for ( int j = 0 ; j < N ; j++)
{
r_xor = r_xor^mat[i][j];
c_xor = c_xor^mat[j][i];
}
if (max_xor < Math.max(r_xor, c_xor))
max_xor = Math.max(r_xor, c_xor);
}
return max_xor;
}
public static void main (String[] args)
{
int N = 3 ;
int mat[][] = { { 1 , 5 , 4 },
{ 3 , 7 , 2 },
{ 5 , 9 , 10 }};
System.out.print( "maximum XOR value : "
+ maxXOR(mat, N));
}
}
|
Python3
MAX = 1000
def maxXOR(mat, N):
max_xor = 0
for i in range (N):
r_xor = 0
c_xor = 0
for j in range (N):
r_xor = r_xor ^ mat[i][j]
c_xor = c_xor ^ mat[j][i]
if (max_xor < max (r_xor, c_xor)):
max_xor = max (r_xor, c_xor)
return max_xor
N = 3
mat = [[ 1 , 5 , 4 ],
[ 3 , 7 , 2 ],
[ 5 , 9 , 10 ]]
print ( "maximum XOR value : " ,
maxXOR(mat, N))
|
C#
using System;
class GFG
{
static int maxXOR( int [,]mat, int N)
{
int r_xor, c_xor;
int max_xor = 0;
for ( int i = 0 ; i < N ; i++)
{
r_xor = 0; c_xor = 0;
for ( int j = 0 ; j < N ; j++)
{
r_xor = r_xor^mat[i, j];
c_xor = c_xor^mat[j, i];
}
if (max_xor < Math.Max(r_xor, c_xor))
max_xor = Math.Max(r_xor, c_xor);
}
return max_xor;
}
public static void Main ()
{
int N = 3;
int [,]mat = { {1, 5, 4},
{3, 7, 2},
{5, 9, 10}};
Console.Write( "maximum XOR value : "
+ maxXOR(mat, N));
}
}
|
PHP
<?php
$MAX = 1000;
function maxXOR( $mat , $N )
{
$r_xor ; $c_xor ;
$max_xor = 0;
for ( $i = 0 ; $i < $N ; $i ++)
{
$r_xor = 0; $c_xor = 0;
for ( $j = 0 ; $j < $N ; $j ++)
{
$r_xor = $r_xor ^ $mat [ $i ][ $j ];
$c_xor = $c_xor ^ $mat [ $j ][ $i ];
}
if ( $max_xor < max( $r_xor , $c_xor ))
$max_xor = max( $r_xor , $c_xor );
}
return $max_xor ;
}
$N = 3;
$mat = array ( array (1, 5, 4),
array (3, 7, 2),
array (5, 9, 10));
echo "maximum XOR value : "
, maxXOR( $mat , $N );
?>
|
Javascript
<script>
const MAX = 1000;
function maxXOR(mat, N)
{
let r_xor, c_xor;
let max_xor = 0;
for (let i = 0 ; i < N ; i++)
{
r_xor = 0, c_xor = 0;
for (let j = 0 ; j < N ; j++)
{
r_xor = r_xor^mat[i][j];
c_xor = c_xor^mat[j][i];
}
if (max_xor < Math.max(r_xor, c_xor))
max_xor = Math.max(r_xor, c_xor);
}
return max_xor;
}
let N = 3;
let mat = [[1 , 5, 4],
[3 , 7, 2 ],
[5 , 9, 10]];
document.write( "maximum XOR value : "
+ maxXOR(mat, N));
</script>
|
Output
maximum XOR value : 12
Time complexity : O(N*N)
space complexity : O(1)
Share your thoughts in the comments
Please Login to comment...