Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | C # implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Driver Code ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int M = 3 ; static int N = 3 ; static int minchanges ( int [ , ] mat ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { if ( mp . ContainsKey ( mat [ i , j ] ) ) { mp [ mat [ i , j ] ] ++ ; } else { mp [ mat [ i , j ] ] = 1 ; } totalsize ++ ; } else if ( i + j == right ) { if ( mp . ContainsKey ( mat [ i , j ] ) ) { mp [ mat [ i , j ] ] ++ ; } else { mp [ mat [ i , j ] ] = 1 ; } totalsize ++ ; } } } int changes = 0 ; foreach ( KeyValuePair < int , int > itr in mp ) { changes = Math . Max ( changes , itr . Value ) ; } count += totalsize - changes ; left ++ ; right -- ; } return count ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; Console . Write ( minchanges ( mat ) ) ; } }