Open In App

Find maximum path sum in a 2D matrix when exactly two left moves are allowed

Last Updated : 31 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a 2D matrix arr[][] of dimensions N * M where N is number of rows and M is number of columns.The task is to find maximum path sum in this matrix satisfying some condition which are as follows :

  1. We can only start with arr[i][M] where 0 <= i <= N.
  2. We end the path on the same side, such that we can take exactly 2 left turns.
    1. First we calculate suffix sum in each row and store it in another 2D matrix call it b[][]so that at every valid index we get the sum of the entire row starting from that index.

b[i][j] = arr[i][j] + b[i][j + 1]

  1. Now we check each consecutive two rows and find the sum of their corresponding columns and simultaneously updating the maximum sum variable. Till now we have found both horizontal lines from that above structure.

sum = max(sum, b[i][j] + b[i – 1][j])

  1. We need to find that vertical line connecting these horizontal lines i.e. column.
  2. After traversing each row, for each valid index we have two choices either we link this index to corresponding index of upper row i.e. add in previous column or start a new column. Whichever value is maximum we retain that value and we update the value at this index.

b[i][j] = max(b[i][j], b[i – 1][j] + arr[i][j])


Similar Reads

Minimum Cost Path with Left, Right, Bottom and Up moves allowed
Given a two-dimensional grid, each cell of which contains an integer cost which represents a cost to traverse through that cell, we need to find a path from the top left cell to the bottom right cell by which the total cost incurred is minimum. Note: It is assumed that negative cost cycles do not exist in input matrix. This problem is an extension
14 min read
Print all paths from top left to bottom right in a matrix with four moves allowed
The problem is to print all the possible paths from top left to bottom right of an mXn matrix with the constraints that from each cell you can either move up, right, left or down. Examples: Input : 1 2 3 4 5 6 Output : 1 2 3 6 1 2 5 6 1 4 5 6 4 5 2 3 6 Input : 1 2 3 4 5 6 7 8 9 Output : 1 2 3 6 9 1 2 3 6 5 8 9 1 2 3 6 5 4 7 8 9 1 2 5 6 9 1 2 5 8 9
9 min read
Maximum sum path in a matrix from top-left to bottom-right
Given a matrix mat[][] of dimensions N * M, the task is to find the path from the top-left cell (0, 0) to the bottom-right cell (N - 1, M - 1) of the given matrix such that sum of the elements in the path is maximum. The only moves allowed from any cell (i, j) of the matrix are (i + 1, j) or (i, j + 1). Examples: Input: mat[][] = {{3, 7}, {9, 8}}Ou
12 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 coordinates[][2]. The only moves allowed from any cell (i
15 min read
Find if path from top-left to bottom-right with sum X exists in given Matrix
Given a matrix A[][] of size N * N, where each matrix element is either -1 or 1. One can move from (X, Y) to either (X + 1, Y) or (X, Y + 1) but not out of the matrix. The task is to check whether a path exists from the top-left to the bottom-right corner such that the sum of all elements in the path is X. Examples: Input: arr[][] = {{1, 1, 1, 1},
13 min read
Maximum non-negative product of a path from top left to bottom right of given Matrix
Given an integer matrix mat[][] of dimensions N * M, the task is to print the maximum product of matrix elements in the 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) is (i + 1, j) or (i, j + 1). If the maximum product is negative, then print "-1". Examples: I
10 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: Bitwise XOR value of a path is defined as the bitwise
9 min read
Check if a path exists from start to end cell in given Matrix with obstacles in at most K moves
Given a positive integer K and a matrix grid of dimensions N * M consisting of characters '.' and '#', where '.' represents the unblocked cells and '#' represents the blocked cells, the task is to check if the bottom-right of the grid can be reached from the top-left cell of the matrix through unblocked cells in at most K moves or not such that it
9 min read
Find ways to arrange K green balls among N balls such that exactly i moves is needed to collect all K green balls
Given two integers N and K. There are N balls placed in a row. K of them are green and N - K of them are black. The task is to find the number of ways to arrange N balls such that one will need exactly i ( 1 ? i ? K ) moves to collect all the green balls. In one move, we can collect any group of consecutive green balls. Note that the answer can be
10 min read
Maximize path sum from top-left cell to all other cells of a given Matrix
Given a matrix, mat[][] of dimensions N * M, the task is to find the maximum path sum from the top-left cell (0, 0) to all other cells of the given matrix. Only possible moves from any cell (i, j) is (i + 1, j) and (i, j + 1). Examples: Input: mat[][] = {{3, 2, 1}, {6, 5, 4}, {7, 8, 9}}Output:3 5 69 14 1816 24 33Explanation:Path from (0, 0) to (0,
8 min read