Kth highest XOR of diagonal elements from a Matrix | Java Program to implement the above approach ; Function to find K - th maximum XOR of any diagonal in the matrix ; Number or rows ; Number of columns ; Store XOR of diagonals ; Traverse each diagonal ; Starting column of diagonal ; Count total elements in the diagonal ; Store XOR of current diagonal ; Push XOR of current diagonal ; Sort XOR values of diagonals ; Print the K - th Maximum XOR ; Driver Code
import java . util . * ; class GFG { static void findXOR ( int [ ] [ ] mat , int K ) { int N = mat . length ; int M = mat [ 0 ] . length ; ArrayList < Integer > digXOR = new ArrayList < Integer > ( ) ; for ( int l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { int s_col = Math . max ( 0 , l - N ) ; int count = Math . min ( l , Math . min ( ( M - s_col ) , N ) ) ; int currXOR = 0 ; for ( int j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ Math . min ( N , l ) - j - 1 ] [ s_col + j ] ) ; } digXOR . add ( currXOR ) ; } Collections . sort ( digXOR ) ; System . out . print ( digXOR . get ( N + M - 1 - K ) ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int K = 3 ; findXOR ( mat , K ) ; } }