VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-xor-value-matrix/

⇱ Maximum XOR value in matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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 . 

  1. 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.
  2. Store the maximum of both.

Below is the implementation : 


Output
maximum XOR value : 12

Time complexity : O(N*N) 
space complexity : O(1)

Comment
Article Tags: