Maximum Path Sum in a Matrix
Given an N*M matrix filled with integer numbers, determine the maximum sum that can be obtained from a path starting from any cell in the first row to any cell in the last row.
You can move from a cell in the current row to another cell directly below, or diagonally below left or right.
Input:
The first line contains an integer 'T', the number of test cases. For each test case:
The first line contains two integers 'N' and 'M', representing the matrix dimensions.
The next 'N' lines contain 'M' space-separated integers representing the matrix elements.
Output:
For each test case, print the maximum sum that can be obtained by following a path as described. Output a separate line for each test case.
Example:
Input:
2
2 3
1 2 3
4 5 6
2 2
9 10
-1 -2
Output:
11
10
Constraints:
- 1 <= T <= 50
- 1 <= N <= 100
- 1 <= M <= 100
- -104 <= matrix[i][j] <= 104
- Time Limit: 1 second
Note:
You do not need to print anything. It has already been taken care of.

AnswerBot
4mo
Find the maximum sum that can be obtained from a path in a matrix from the first row to the last row.
Use dynamic programming to keep track of the maximum sum at each cell in the matrix.
Consider moving...read more
Help your peers!
Add answer anonymously...
Traveloka Software Developer interview questions & answers
A Software Developer was asked Q. Search Element in a Rotated Sorted Array Given a sorted array that has been rota...read more
A Software Developer was asked Q. Partition Equal Subset Sum Problem Given an array ARR consisting of 'N' positive...read more
A Software Developer was asked Q. Maximum Path Sum in a Matrix Given an N*M matrix filled with integer numbers, de...read more
Popular interview questions of Software Developer
A Software Developer was asked Q1. Search Element in a Rotated Sorted Array Given a sorted array that has been rota...read more
A Software Developer was asked Q2. Partition Equal Subset Sum Problem Given an array ARR consisting of 'N' positive...read more
A Software Developer was asked Q3. Maximum Path Sum in a Matrix Given an N*M matrix filled with integer numbers, de...read more
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

