Kth highest XOR of diagonal elements from a Matrix | 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
function findXOR ( mat , K ) { let N = mat . length ; let M = mat [ 0 ] . length ; let digXOR = [ ] ; for ( let l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { let s_col = Math . max ( 0 , l - N ) ; let count = Math . min ( l , ( M - s_col ) , N ) ; let currXOR = 0 ; for ( let j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ Math . min ( N , l ) - j - 1 ] [ s_col + j ] ) ; } digXOR . push ( currXOR ) ; } digXOR . sort ( ( a , b ) => a - b ) ; document . write ( digXOR [ N + M - 1 - K ] ) ; } let mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; let K = 3 ; findXOR ( mat , K ) ;