![]() |
VOOZH | about |
Given a 2D square matrix arr[][] of dimensions N x N, the task is to find the maximum path sum by moving diagonally from any cell and each cell must be visited only once i.e., from the cell (i, j), a player can move to the cell (i + 1, j + 1).
Examples:
Input: arr[][] = {{1, 2, 3}, {3, 5, 10}, {1 3 5}}
Output: 12
Explanation:
Sum of cells (1, 1), (2, 2) and (3, 3) is 11.
The sum of cells (1, 2), (2, 3) and (1, 3) is 3.
The sum of cells (2, 1) and (3, 2) is 6.
The sum of cell (3, 1) is 1.
The maximum possible sum is 12.Input: arr[][] = {{1, 1, 1}, {1 1 1}, {1 1 1}}
Output: 3
Approach: To solve this problem, the idea is to traverse the matrix diagonally for first row and column elements and sum up their diagonal elements within the range of the matrix.
Follow the steps below to solve the problem:
Below is the implementation of the above approach:
12
Time Complexity: O(N2)
Auxiliary Space: O(1)