text: Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2 | C # program for the above approach ; Function for counting minimum number of changes ; Distance of elements from ( 0 , 0 ) will is i range [ 0 , n + m - 2 ] ; Store frequencies of [ 0 , 9 ] at distance i ; Initialize frequencies as 0 ; Count frequencies of [ 0 , 9 ] ; Increment frequency of value matrix [ i , j ] at distance i + j ; Find value with max frequency and count total cells at distance i from front end and rear end ; Change all values to the value with max frequency ; Return the answer ; Driver Code ; Given matrix ; Function call
code: using System ; class GFG { static readonly int N = 7 ; static int countChanges ( int [ , ] matrix , int n , int m ) { int dist = n + m - 1 ; int [ , ] freq = new int [ dist , 10 ] ; for ( int i = 0 ; i < dist ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) freq [ i , j ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { freq [ i + j , matrix [ i , j ] ] ++ ; } } int min_changes_sum = 0 ; for ( int i = 0 ; i < dist / 2 ; i ++ ) { int maximum = 0 ; int total_values = 0 ; for ( int j = 0 ; j < 10 ; j ++ ) { maximum = Math . Max ( maximum , freq [ i , j ] + freq [ n + m - 2 - i , j ] ) ; total_values += ( freq [ i , j ] + freq [ n + m - 2 - i , j ] ) ; } min_changes_sum += ( total_values - maximum ) ; } return min_changes_sum ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 2 } , { 3 , 5 } } ; Console . Write ( countChanges ( mat , 2 , 2 ) ) ; } }