Maximum XOR value in matrix - GeeksforGeeks
Given a square matrix (N X N), the task is to find the maximum XOR value of a complete row or a complete column.
Read full article from Maximum XOR value in matrix - GeeksforGeeks
Given a square matrix (N X N), the task is to find the maximum XOR value of a complete row or a complete column.
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.
const int MAX = 1000;// function return the maximum xor value that is// either row or column wiseint maxXOR(int mat[][MAX], int N){ // for row xor and column xor int r_xor, c_xor; int max_xor = 0; // traverse matrix for (int i=0 ; i<N ; i++) { r_xor = 0, c_xor = 0; for (int j=0 ; j< N ; j++) { // xor row element r_xor = r_xor^mat[i][j]; // for each column : j is act as row & i // act as column xor column element c_xor = c_xor^mat[j][i]; } // update maximum between r_xor , c_xor if (max_xor < max(r_xor,c_xor)) max_xor = max(r_xor,c_xor); } // return maximum xor value return max_xor;}Read full article from Maximum XOR value in matrix - GeeksforGeeks