# Find maximum sum from top to bottom row with no adjacent diagonal elements

Given a matrix A[][] of N * M, the task is to find the maximum sum from the top row to the bottom row after selecting one element from each row with no adjacent diagonal element.

Examples:

Input: A = { {1, 2, 3, 4}, {8, 7, 6, 5}, {10, 11, 12, 13} }
Output: 25
Explanation:
Selected elements to give maximum sum –
Row 0 = 4
Row 1 = 8
Row 2 = 13
Sum = 25

Input: A = { {1, 6}, {5, 3}, {11, 7} }
Output: 17
Explanation:
Selected elements to give maximum sum –
Row 0 = 1
Row 1 = 5
Row 2 = 11

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Explanation: For Selecting any element if we have selected A[i][j], then elements A[i+1][j+1] and A[i+1][j-1] cannot be selected.
In the Given example Select the maximum element from the top-row where 4 is maximum in this case, then element A[1][2] cannot be selected which is 6, select the element 8 which maximum from the availiable options. Similiary element 11 cannot be selected from the 3rd row. Select the element 13 to get the maximum sum which is 25.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: Generate all the combinations of N elements after choosing 1 element from every row and select the combination which produces maximum sum.
Efficient Approach: The idea is to use the concept of Dynamic programming in bottom up manner. Begin with the bottom most row of the given matrix and repeat the below process untill we reach the top most row.

• Create an auxilary array of the bottom most row elements with it corresponding indexes.
• Sort the auxilary array.
• Iterate over the auxillary array and for each element find maximum element from the row above to be added to the current element to produce maximum sum such that for every A[i][j] the selected element is not A[i-1][j+1] or A[i-1][j-1].
• Repeat this until reach to the topmost row of the given matrix array.
• Find the maximum element from the top row to get the maximum sum

Below is the implementation of the above approach.

 `// C++ implementation to find ` `// maximum sum from top to bottom ` `// row with no adjacent diagonal elements ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find the maximum ` `// path sum from top to bottom row ` `int` `maxSum(vector >& V, ` `                       ``int` `n, ``int` `m){ ` `    ``int` `ans = 0; ` `    ``for` `(``int` `i = n - 2; i >= 0; --i) { ` `        ``// Create an auxilary array of next row ` `        ``// with the element and it's position ` `        ``vector > aux; ` ` `  `        ``for` `(``int` `j = 0; j < m; ++j) { ` `            ``aux.push_back({ V[i + 1][j], ` `                                    ``j }); ` `        ``} ` ` `  `        ``// Sort the auxilary array ` `        ``sort(aux.begin(), aux.end()); ` `        ``reverse(aux.begin(), aux.end()); ` ` `  `        ``// Find maximum from row above to ` `        ``// be added to the current element ` `        ``for` `(``int` `j = 0; j < m; ++j) { ` `             `  `            ``// Find the maximum element from ` `            ``// the next row that can be added ` `            ``// to current row element ` `            ``for` `(``int` `k = 0; k < m; ++k) { ` `                ``if` `(aux[k].second - j == 0 || ` `                   ``abs``(aux[k].second - j) > 1) { ` `                    ``V[i][j] += aux[k].first; ` `                    ``break``; ` `                ``} ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Find the maximum sum ` `    ``for` `(``int` `i = 0; i < m; ++i) { ` `        ``ans = max(ans, V[0][i]); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``vector > V{{ 1, 2, 3, 4 }, ` `                           ``{ 8, 7, 6, 5 }, ` `                           ``{ 10, 11, 12, 13 }}; ` `    ``int` `n = V.size(); ` `    ``int` `m = V[0].size(); ` ` `  `    ``// Function to find maximum path ` `    ``cout << maxSum(V, n, m); ` ` `  `    ``return` `0; ` `} `

 `# Python3 implementation to find ` `# maximum sum from top to bottom ` `# row with no adjacent diagonal elements ` ` `  `# Function to find the maximum ` `# path sum from top to bottom row ` `def` `maxSum(V, n, m): ` `    ``ans ``=` `0` `    ``for` `i ``in` `range``(n ``-` `2``, ``-``1``, ``-``1``): ` `         `  `        ``# Create an auxilary array of next row ` `        ``# with the element and it's position ` `        ``aux ``=` `[] ` ` `  `        ``for` `j ``in` `range``(m): ` `            ``aux.append([V[i ``+` `1``][j], j]) ` ` `  `        ``# Sort the auxilary array ` `        ``aux ``=` `sorted``(aux) ` `        ``aux ``=` `aux[::``-``1``] ` ` `  `        ``# Find maximum from row above to ` `        ``# be added to the current element ` `        ``for` `j ``in` `range``(m): ` ` `  `            ``# Find the maximum element from ` `            ``# the next row that can be added ` `            ``# to current row element ` `            ``for` `k ``in` `range``(m): ` `                ``if` `(aux[k][``1``] ``-` `j ``=``=` `0` `or` `                ``abs``(aux[k][``1``] ``-` `j) > ``1``): ` `                    ``V[i][j] ``+``=` `aux[k][``0``] ` `                    ``break` ` `  `    ``# Find the maximum sum ` `    ``for` `i ``in` `range``(m): ` `        ``ans ``=` `max``(ans, V[``0``][i]) ` ` `  `    ``return` `ans ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` ` `  `    ``V``=``[[ ``1``, ``2``, ``3``, ``4` `], ` `    ``[ ``8``, ``7``, ``6``, ``5` `], ` `    ``[ ``10``, ``11``, ``12``, ``13``]] ` `    ``n ``=` `len``(V) ` `    ``m ``=` `len``(V[``0``]) ` ` `  `    ``# Function to find maximum path ` `    ``print``(maxSum(V, n, m)) ` ` `  `# This code is contributed by mohit kumar 29 `

Output:
```25
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29

Article Tags :