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
def findXOR ( mat , K ) : NEW_LINE INDENT N = len ( mat ) NEW_LINE M = len ( mat [ 0 ] ) NEW_LINE digXOR = [ ] NEW_LINE for l in range ( 1 , N + M , 1 ) : NEW_LINE INDENT s_col = max ( 0 , l - N ) NEW_LINE count = min ( [ l , ( M - s_col ) , N ] ) NEW_LINE currXOR = 0 NEW_LINE for j in range ( count ) : NEW_LINE INDENT currXOR = ( currXOR ^ mat [ min ( N , l ) - j - 1 ] [ s_col + j ] ) NEW_LINE DEDENT digXOR . append ( currXOR ) NEW_LINE DEDENT digXOR . sort ( reverse = False ) NEW_LINE print ( digXOR [ N + M - 1 - K ] ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE K = 3 NEW_LINE findXOR ( mat , K ) NEW_LINE DEDENT