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 ; Drive Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  3 NEW_LINE #define N  3 NEW_LINE int minchanges ( int mat [ N ] [ M ] ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { unordered_map < int , int > mp ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { mp [ mat [ i ] [ j ] ] ++ ; totalsize ++ ; } else if ( i + j == right ) { mp [ mat [ i ] [ j ] ] ++ ; totalsize ++ ; } } } unordered_map < int , int > :: iterator itr = mp . begin ( ) ; int changes = 0 ; for ( ; itr != mp . end ( ) ; itr ++ ) changes = max ( changes , itr -> second ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } int main ( ) { int mat [ ] [ M ] = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; cout << minchanges ( mat ) ; return 0 ; }