Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Javascript 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 ; Drive Code ; Function Call
var M = 3 var N = 3 ; function minchanges ( mat ) { var count = 0 ; var left = 0 , right = N + M - 2 ; while ( left < right ) { var mp = new Map ( ) ; var totalsize = 0 ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = 0 ; j < M ; j ++ ) { if ( i + j == left i + j == right ) { if ( mp . has ( mat [ i ] [ j ] ) ) mp . set ( mat [ i ] [ j ] , mp . get ( mat [ i ] [ j ] ) + 1 ) else mp . set ( mat [ i ] [ j ] , 1 ) totalsize ++ ; } } } var changes = 0 ; mp . forEach ( ( value , key ) => { changes = Math . max ( changes , value ) ; } ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } var mat = [ [ 1 , 4 , 1 ] , [ 2 , 5 , 3 ] , [ 1 , 3 , 1 ] ] ; document . write ( minchanges ( mat ) ) ;