Kth highest XOR of diagonal elements from a Matrix | C # 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
using System ; using System . Collections . Generic ; class GFG { static void findXOR ( int [ , ] mat , int K ) { int N = mat . GetLength ( 0 ) ; int M = mat . GetLength ( 1 ) ; List < int > digXOR = new List < int > ( ) ; 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 ) ; } digXOR . Sort ( ) ; Console . Write ( digXOR [ 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 ) ; } }