Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
Last Updated :
24 Mar, 2023
Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell.
Examples:
Input: mat[][] = {{1, 2}, {3, 1}}
Output: 0
Explanation:
Every path in the matrix from top left to bottom right is palindromic.
Paths => {1, 2, 1}, {1, 3, 1}
Input: mat[][] = {{1, 2}, {3, 5}}
Output: 1
Explanation:
Only one change is required for the every path to be palindromic.
That is => mat[1][1] = 1
Paths => {1, 2, 1}, {1, 3, 1}
Naive Approach: For the naive approach please refer to this post.
Efficient Approach: The idea is to discard the use of an extra space that is the use of HashMap. Follow the steps given below:
- Distance possible from top left and bottom right are in the range 0 to N + M - 2. Hence create a 2D array of dimensions [N + M - 1][10].
- Store frequency of distances in an array while considering Row number (in range 0 to N + M - 2) as distance and column number (0 to 9) as an element in the given matrix.
- For the number of changes to be minimum, change each cell at distance X with a value that has the highest frequency among all values at distance X.
- The minimum number of steps required is the sum of the difference of total values of frequency and the maximum value of frequency for each distance.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define N 7
// Function for counting minimum
// number of changes
int countChanges(int matrix[][N],
int n, int m)
{
// Distance of elements from (0, 0)
// will is i range [0, n + m - 2]
int dist = n + m - 1;
// Store frequencies of [0, 9]
// at distance i
int freq[dist][10];
// Initialize frequencies as 0
for (int i = 0; i < dist; i++) {
for (int j = 0; j < 10; j++)
freq[i][j] = 0;
}
// Count frequencies of [0, 9]
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
// Increment frequency of
// value matrix[i][j]
// at distance i+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;
// Find value with max frequency
// and count total cells at distance i
// from front end and rear end
for (int j = 0; j < 10; j++) {
maximum = max(maximum, freq[i][j]
+ freq[n + m - 2 - i][j]);
total_values += (freq[i][j]
+ freq[n + m - 2 - i][j]);
}
// Change all values to the
// value with max frequency
min_changes_sum += (total_values
- maximum);
}
// Return the answer
return min_changes_sum;
}
// Driver Code
int main()
{
// Given Matrix
int mat[][N] = { { 1, 2 }, { 3, 5 } };
// Function Call
cout << countChanges(mat, 2, 2);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static final int N = 7;
// Function for counting minimum
// number of changes
static int countChanges(int matrix[][],
int n, int m)
{
// Distance of elements from (0, 0)
// will is i range [0, n + m - 2]
int dist = n + m - 1;
// Store frequencies of [0, 9]
// at distance i
int [][]freq = new int[dist][10];
// Initialize frequencies as 0
for(int i = 0; i < dist; i++)
{
for(int j = 0; j < 10; j++)
freq[i][j] = 0;
}
// Count frequencies of [0, 9]
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
// Increment frequency of
// value matrix[i][j]
// at distance i+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;
// Find value with max frequency
// and count total cells at distance i
// from front end and rear end
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]);
}
// Change all values to the
// value with max frequency
min_changes_sum += (total_values -
maximum);
}
// Return the answer
return min_changes_sum;
}
// Driver Code
public static void main(String[] args)
{
// Given matrix
int mat[][] = { { 1, 2 }, { 3, 5 } };
// Function call
System.out.print(countChanges(mat, 2, 2));
}
}
// This code is contributed by Rohit_ranjan
Python3
# Python3 program for the above approach
# Function for counting minimum
# number of changes
def countChanges(matrix, n, m):
# Distance of elements from (0, 0)
# will is i range [0, n + m - 2]
dist = n + m - 1
# Store frequencies of [0, 9]
# at distance i
# Initialize all with zero
freq = [[0] * 10 for i in range(dist)]
# Count frequencies of [0, 9]
for i in range(n):
for j in range(m):
# Increment frequency of
# value matrix[i][j]
# at distance i+j
freq[i + j][(matrix[i][j])] += 1
min_changes_sum = 0
for i in range(dist // 2):
maximum = 0
total_values = 0
# Find value with max frequency
# and count total cells at distance i
# from front end and rear end
for j in range(10):
maximum = max(maximum, freq[i][j] +
freq[n + m - 2 - i][j])
total_values += (freq[i][j] +
freq[n + m - 2 - i][j])
# Change all values to the value
# with max frequency
min_changes_sum += (total_values -
maximum)
# Return the answer
return min_changes_sum
# Driver code
if __name__ == '__main__':
# Given matrix
mat = [ [ 1, 2 ], [ 3, 5 ] ]
# Function call
print(countChanges(mat, 2, 2))
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
class GFG{
//static readonly int N = 7;
// Function for counting minimum
// number of changes
static int countChanges(int [,]matrix,
int n, int m)
{
// Distance of elements from (0, 0)
// will is i range [0, n + m - 2]
int dist = n + m - 1;
// Store frequencies of [0, 9]
// at distance i
int [,]freq = new int[dist, 10];
// Initialize frequencies as 0
for(int i = 0; i < dist; i++)
{
for(int j = 0; j < 10; j++)
freq[i, j] = 0;
}
// Count frequencies of [0, 9]
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
// Increment frequency of
// value matrix[i,j]
// at distance i+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;
// Find value with max frequency
// and count total cells at distance i
// from front end and rear end
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]);
}
// Change all values to the
// value with max frequency
min_changes_sum += (total_values -
maximum);
}
// Return the answer
return min_changes_sum;
}
// Driver Code
public static void Main(String[] args)
{
// Given matrix
int [,]mat = { { 1, 2 }, { 3, 5 } };
// Function call
Console.Write(countChanges(mat, 2, 2));
}
}
// This code is contributed by Rohit_ranjan
JavaScript
<script>
// JavaScript program for the above approach
var N = 7;
// Function for counting minimum
// number of changes
function countChanges(matrix, n, m)
{
// Distance of elements from (0, 0)
// will is i range [0, n + m - 2]
var dist = n + m - 1;
// Store frequencies of [0, 9]
// at distance i
var freq = Array.from(Array(dist), ()=>Array(10));
// Initialize frequencies as 0
for (var i = 0; i < dist; i++) {
for (var j = 0; j < 10; j++)
freq[i][j] = 0;
}
// Count frequencies of [0, 9]
for (var i = 0; i < n; i++) {
for (var j = 0; j < m; j++) {
// Increment frequency of
// value matrix[i][j]
// at distance i+j
freq[i + j][(matrix[i][j])]++;
}
}
var min_changes_sum = 0;
for (var i = 0; i < parseInt(dist / 2); i++) {
var maximum = 0;
var total_values = 0;
// Find value with max frequency
// and count total cells at distance i
// from front end and rear end
for (var 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]);
}
// Change all values to the
// value with max frequency
min_changes_sum += (total_values
- maximum);
}
// Return the answer
return min_changes_sum;
}
// Driver Code
// Given Matrix
var mat = [[1, 2], [3, 5 ]];
// Function Call
document.write( countChanges(mat, 2, 2));
</script>
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Similar Reads
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2 Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell. Examples: Input: M
12 min read
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell.Examples: Input: ma
15+ min read
Minimum cost to reach from the top-left to the bottom-right corner of a matrix Given an N * M matrix mat[][] consisting of lower case characters, the task is to find the minimum cost to reach from the cell mat[0][0] to the cell mat[N-1][M-1]. If you are at a cell mat[i][j], you can jump to the cells mat[i+1][j], mat[i][j+1], mat[i-1][j], mat[i][j-1] (without going out of bound
11 min read
Print all palindromic paths from top left to bottom right in a matrix Given a m*n matrix mat[][] containing only lowercase alphabetical characters, the task is to print all palindromic paths from the top-left cell to the bottom-right cell. A path is defined as a sequence of cells starting from the top-left and ending at the bottom-right, and we can only move right or
7 min read
Maximum path sum from top left to bottom right of a matrix passing through one of the given cells Given a matrix mat[][] of dimensions N * M and a set of coordinates of cell coordinates[][] of size Q, the task is to find the maximum sum of a path from the top-left cell (1, 1) to the bottom-right cell (N, M), such that the path should contain at least one of the coordinates from the array coordin
15 min read
Maximum XOR of a path from top-left to bottom-right cell of given Matrix Given a matrix, mat[][] of dimensions N * M, the task is to print the maximum bitwise XOR value that can be obtained for a path from the top-left cell (0, 0) to the bottom-right cell (N - 1, M - 1) of the given matrix. Only possible moves from any cell (i, j) are (i + 1, j) and (i, j + 1). Note: Bit
9 min read