Matrix Max Sum Path Problem in Java6 Jan 2025 | 9 min read It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve matrix max sum path problem with different approaches and logic. Also, we will create Java programs for the same. Problem StatementWe have given a matrix of n×m. In this matrix, we have to find the max path sum first. The movement in matrix is allowed form the top-left corner to the bottom-right corner. The movement direction must be right, downwards, or diagonally right (from (i, j) to (i+1, j), (i, j+1), or (i+1, j+1)). Solution to the ProblemTo find the max path sum first, we have to find the max value in the first row of the matrix. Store this value in res variable. Now for every element in the matrix update element with max value that can be included in max path. If the value is greater then res variable, then update the res variable. At last return res variable that consists of max path sum value. Let's understand it through an example. Matrix Max Sum Path ExampleInput Matrix: Output: 500 Explanation: Example 2: Input Matrix: Output: 3000 Explanation: Example 3: Input Matrix: Output: 2300 Explanation: We can solve the above problem by using the following three approaches.
Approach: Using RecursionThe naive approach using recursion for the Matrix Max Sum Path Problem involves recursively exploring all possible paths from the top to the bottom of a matrix, calculating the maximum sum path without utilizing memoization to store computed results. Algorithm:Step 1: Define a MatrixMaxSumPathNaive class with a main method to initialize a sample integer matrix. Step 2: Create a method maxSumPath that takes a 2D integer array matrix as input. Determine the number of rows rows and columns cols in the matrix. Begin recursion by calling maxSumPathRecursive(matrix, 0, 0, rows, cols). Step 3: Define a private static method maxSumPathRecursive to recursively find the maximum sum path. Parameters include matrix, current row, current col, total rows, and total cols.
Step 4: Calculate maximum path sum by:
Step 5: Return maxPathSum, the maximum sum path found from the current cell. Step 6: After calling maxSumPath(matrix) in main, print the result to show the maximum sum path found in the matrix using the naive recursive approach. Let's implement the above steps in a Java program. Implementation of Matrix Max Path Sum Problem Using RecursionFile Name: MatrixMaxSumPathNaive.java Output: Maximum sum path in the matrix (naive approach): 17 Time Complexity: The time complexity of the naive recursive approach is O(3^{n}), where n is the number of rows in the matrix. It is because, from each cell, the function can move in three possible directions (down, down-left, down-right), leading to an exponential number of recursive calls. Auxiliary Space: The auxiliary space is O(n), where n is the number of rows. The space is used by the recursion stack, with the maximum depth of recursion being equal to the number of rows in the worst case. Approach: Dynamic ProgrammingDynamic programming is a problem-solving technique where solutions to subproblems are stored and reused to efficiently solve larger instances of the problem. It optimally solves complex problems by breaking them down into simpler overlapping subproblems, often using a table (memoization) or bottom-up approach to build solutions iteratively. Algorithm:Step 1: Define MAX_SIZE for the maximum size of the matrix and declare numRows, numCols, matrix, dp, and visited arrays. Step 2: Implement inputMatrix() to set numRows, numCols, and populate matrix with values. Step 3: Define maximumSumPath(int row, int col) to compute the maximum sum path from cell (row, col). Step 4: If at bottom-right corner, return its value. If cell is visited, return stored value from dp. Mark as Visited and Set visited[row][col] to 1. Step 5: Initialize totalSum. If not on the last row or column, calculate the maximum sum from moving right, diagonally down-right, or down.
Step 6: Return the updated maximum sum value for the current cell. Step 7: Call inputMatrix(). Call maximumSumPath(0, 0) to find the maximum sum path from the top-left corner and print the result. Let's implement the above steps in a Java program. Implementation of Matrix Max Path Sum Problem Using Dynamic ProgrammingFile Name: MatrixMaxSumPath.java Output: Maximum sum path in the matrix: 29 Time Complexity: The time complexity is O(n×m), as each cell is processed at most once due to memoization. Auxiliary Space: The auxiliary space is O(n×m), for the dp and visited arrays. Approach: Using TabulationIn this approach, a dynamic programming (DP) table is used to iteratively compute the maximum sum path from the bottom-right corner to the top-left corner of the matrix. The bottom-up method avoids redundant calculations and ensures optimal substructure by building the solution incrementally. Algorithm:Step 1: Define a function maxSumPath(int[][] matrix) to find the maximum sum path in a given matrix. Step 2: Initialize variables rows and cols to store the number of rows and columns in matrix and create a 2D array dp of size rows x cols to store the maximum sums. Step 3: Set dp[rows - 1][cols - 1] to matrix[rows - 1][cols - 1], representing the bottom-right corner of the matrix. Step 4: Iterate over the last row and column of dp to calculate maximum sums:
Step 5: Iterate through the rest of the dp table from bottom-right to top-left (i = rows - 2 to 0, j = cols - 2 to 0):
Step 6: The maximum sum path from the top-left corner of the matrix is stored in dp[0][0]. Let's implement the above steps in a Java program. Implementation of Matrix Max Path Sum Problem Using TabulationFile Name: MatrixMaxSumPathTabulation.java Output: Maximum sum path in the matrix (tabulation approach): 35 Time Complexity: The time complexity of the provided code is O(n×m), where n is the number of rows and m is the number of columns in the matrix. It is because each cell in the matrix is processed exactly once. Auxiliary Space: The auxiliary space of the provided code is O(n×m), where n is the number of rows and m is the number of columns in the matrix. It is due to the additional space required for the dynamic programming table (dp) of size n×m. Next TopicMethod-and-block-synchronization-in-java |
Programs need conditional statements to perform decisions based on specific conditions. The if statement belongs among the fundamental control structures of Java programming to help programmers make decisions. Complex conditions require more than one if statement for adequate processing. The programming language achieves such logic through...
4 min read
Asynchronous programming in Java allows tasks to execute independently without hindering the main thread, enhancing performance and responsiveness. It is commonly utilized for managing concurrent operations, background tasks, and I/O processing. Asynchronous Techniques in Java Callbacks and Callback Hell: Callbacks act as reminders, notifying when a task is...
5 min read
Java generics introduced the idea of parameterized types, which completely changed how programmers create Java code. Because of this, programming has entered a new era in which Java code is shorter, more adaptable, and type-safe. To accomplish these advantages, several design patterns make use of Java...
10 min read
C Language C is a middle-level, compiled and general-purpose programming language that follows a top-down approach for developing applications. It was developed at Bell Labs by Dennis Ritchie in 1970 for the Unix Operating System 1970. It is ideal for developing firmware and portable applications. Example #include ...
4 min read
? In Java, analyzing the composition of a string and calculating the percentage of different character types, such as uppercase letters, lowercase letters, digits, and special characters, can be achieved by utilizing string manipulation and character classification methods. This section will guide you through the process step-by-step,...
3 min read
In this section, we will learn what is a pyramidial number and also create Java programs to check if the given number is a pyramidial number or not. The pyramidial number program is frequently asked in Java coding interviews and academics. There are two types of Pyramidal...
18 min read
? Java is a computer language that is widely utilized in many different applications due to its strength and adaptability. But errors happen when coding, just as in any other programming language. Java programmers must be proficient at effectively clearing faults in order to guarantee that their...
4 min read
Playfair cipher is proposed by Charles Whetstone in 1889. But it was named for one of his friends Lord Lyon Playfair because he popularized its uses. It is the most popular symmetric encryption technique that falls under the substitution cipher. It is an encoding procedure that...
9 min read
The getChannel() method is defined in Java.io.FileInputStream class. The getChannel() method is the gateway to creating a FileChannel instance for a file. It is typically available on classes like FileInputStream, FileOutputStream, and RandomAccessFile. FileInputStream We can use FileInputStream to read data from a file. If we need to...
5 min read
In Java, the immutable class can be defined such that when the object of it is created, it's content cannot be changed again. All the wrapper classes in Java such as Integer, Boolean, Byte, Short are immutable. In Java, immutable classes are known for providing various benefits...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India