Check if matrix can be converted to another matrix by transposing square sub | C # implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code
using System ; using System . Collections . Generic ; class GFG { static readonly int n = 3 ; static readonly int m = 3 ; static bool check ( int [ , ] a , int [ , ] b ) { for ( int i = 0 ; i < n ; i ++ ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; int r = i ; int col = 0 ; while ( r >= 0 && col < m ) { v1 . Add ( a [ r , col ] ) ; v2 . Add ( b [ r , col ] ) ; r -- ; col ++ ; } v1 . Sort ( ) ; v2 . Sort ( ) ; for ( int j = 0 ; j < v1 . Count ; j ++ ) { if ( v1 [ j ] != v2 [ j ] ) { return false ; } } } for ( int j = 1 ; j < m ; j ++ ) { List < int > v1 = new List < int > ( ) ; List < int > v2 = new List < int > ( ) ; int r = n - 1 ; int col = j ; while ( r >= 0 && col < m ) { v1 . Add ( a [ r , col ] ) ; v2 . Add ( b [ r , col ] ) ; r -- ; col ++ ; } v1 . Sort ( ) ; v2 . Sort ( ) ; for ( int i = 0 ; i < v1 . Count ; i ++ ) { if ( v1 [ i ] != v2 [ i ] ) { return false ; } } } return true ; } public static void Main ( ) { int [ , ] a = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int [ , ] b = { { 1 , 4 , 7 } , { 2 , 5 , 6 } , { 3 , 8 , 9 } } ; if ( check ( a , b ) ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }