- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum sum path in a matrix from top to bottom and back in C++ Program
In this problem, we are given a matrix mat[][] of size nXm. Our task is to create a program to find the maximum sum path in a matrix from top to bottom and back.
Problem Description − We need to find the maximum path sum from topleft to bottom−right and then back.
Valid moves
From mat[0][0] to mat[n−1][m−1]: Right (mat[i][j] to mat[i][j+1]) and Down (mat[i][j] to mat[i+1][j]). From mat[n−1][m−1] to mat[0][0]: left (mat[i][j] to mat[i][j−1]) and up (mat[i][j] to mat[i−1][j]).
One important thing is that both paths cannot be the same. There should be one or more elements different in both paths.
Let’s take an example to understand the problem,
Input
mat[][] = { {1, 2, 4}, {3, 0, 1}, {5, −1, −1} }
Output
15
Explanation
Path from mat[0][0] to mat[n−1][m−1]: 1 + 3 + 5 − 1 − 1 = 7 Path from mat[n−1][m−1] to mat[0][0]: + 1 + 4 + 2 + 1 = 8 Sum = 7 + 8 = 15
Solution Approach
To solve the problem, we need to find two paths (one from mat[0][0] to mat[n−1][m−1] and another from mat[n−1][m−1] to mat[0][0] ). But a better thing to do is find a sum for two different paths from mat[0][0] to mat[n− 1][m−1]. For this, we will start from mat[0][0] and find two paths by finding the next most promising elements until they reach the end of the path. Then return the sum of both. One thing we need to check is to find if a cell is not on both paths as there need to be two different paths.
Example
Program to illustrate the working of our solution,
#include <bits/stdc++.h> using namespace std; #define row 3 int CalcNodeDiff(int mat[][row], int path1x, int path1y, int path2x, int path2y) { if (path1x == path2x && path1y == path2y) { return mat[path1x][path1y]; } return mat[path1x][path1y] + mat[path2x][path2y]; } int calcMaxPathSumOfMat(int mat[][row], int path1x, int path1y, int path2x, int n) { int pathSumDP[5][5][5]; memset(pathSumDP, −1, sizeof(pathSumDP)); int path2y = path1x + path1y − path2x; int maxPathSum = −10000; if (path1x >= n || path2x >= n || path1y >= row || path2y >= row) return 0; if (pathSumDP[path1x][path1y][path2x] != −1) return pathSumDP[path1x][path1y][path2x]; maxPathSum = max(maxPathSum, calcMaxPathSumOfMat(mat, path1x + 1, path1y, path2x + 1, n) + CalcNodeDiff(mat, path1x, path1y, path2x, path2y)); maxPathSum = max(maxPathSum, calcMaxPathSumOfMat(mat, path1x, path1y + 1, path2x, n) + CalcNodeDiff(mat, path1x, path1y, path2x, path2y)); maxPathSum = max(maxPathSum, calcMaxPathSumOfMat(mat, path1x, path1y + 1, path2x + 1, n) + CalcNodeDiff(mat, path1x, path1y, path2x, path2y)); maxPathSum = max(maxPathSum, calcMaxPathSumOfMat(mat, path1x + 1, path1y, path2x, n) + CalcNodeDiff(mat, path1x, path1y, path2x, path2y)); pathSumDP[path1x][path1y][path2x] = maxPathSum; return maxPathSum; } int main() { int n = 3; int mat[n][row] = { { 1, 2, 4 }, { 3, 0, 1 }, { 5, −1, −1 } }; cout<<"The maximum sum path in a matrix from top to bottom and back is "<<calcMaxPathSumOfMat(mat, 0, 0, 0, n); return 0; }
Output
The maximum sum path in a matrix from top to bottom and back is 15