## Problem Statement

Finding the maximum path sum in a triangle is a very common programming problem. You are given a triangle of numbers which can be represented as below. You are allowed to start from the top of the triangle and move to either of the two number below it. As we move down from the top to the bottom in multiple ways, we keep adding the numbers for each node we visit. The goal is to maximize the sum . A more formal problem statement can be found on the Project Euler website.

## How to approach the problem

At any point you have two choices you can go to the number below the current number or to the adjacent number. An example could be below:

1 2 3 4 5 6 |
3 7 4 2 4 6 8 5 9 3 |

The possible choices we have for the above sample is :

- 3, 7, 2, 8
- 3, 7, 2, 5
- 3, 7, 4, 5
- 3, 7, 4, 9
- 3, 4, 4, 5
- 3, 4, 4, 9
- 3, 4, 6, 9
- 3, 4, 6, 3

Out of the above 8 choices, we calculate that the sum for path 3, 7, 4, 9 is 23 and it is the maximum of all other sums possible.

- The idea is to generate all possible paths from the top to the bottom.
- At every point we have 2 directions to search and we explore every possible path through recursion.
- After getting the result at every point we compare the path results and return on the corresponding data based on the fact which ever is maximum.

## Code Sample for maximum path sum in a triangle

Here is the recursive code which we derived based on the above discussion.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public static int getSum(int level,int x, int y) { // if we reached the last row, return the element at x,y if (level == n-1) { return M[x][y]; } // find the sum considering the first path int ls = getSum(level+1,x + 1, y); // find the sum considering the second path int rs = getSum(level+1,x + 1, y + 1); // return the sum which maximizes the total sum if(ls>rs) return ls + M[x][y]; return rs + M[x][y]; } |

### Storing the input

We store the input in a two dimensional array and the apex of the triangle is stored at (0,0). It might look like a right triangle the way it is stored.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
static int M[][] = { { 75, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, { 95, 64, -1, -1, -1, -1, -1, -1, -1, -1, -1}, { 17, 47, 82, -1, -1, -1, -1, -1, -1, -1, -1}, { 18, 35, 87, 10, -1, -1, -1, -1, -1, -1, -1}, { 20, 04, 82, 47, 65, -1, -1, -1, -1, -1, -1}, { 19, 01, 23, 75, 03, 34, -1, -1, -1, -1, -1}, { 88, 02, 77, 73, 07, 63, 67, -1, -1, -1, -1}, { 99, 65, 04, 28, 06, 16, 70, 92, -1, -1, -1}, { 41, 41, 26, 56, 83, 40, 80, 70, 33, -1, -1}, { 41, 48, 72, 33, 47, 32, 37, 16, 94, 29, -1}, { 53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14} }; |

And finally the getSum method is invoked with the arguments (0,0,0). This means, we start with level 0, and the element in the matrix is at row 0 and column 0.

## Analysis

As we are solving this through recursion and at each level the recursion tree divides itself into two problem of smaller size and does some constant work. The size of the problem is reduced by 1 at each recursive step. So we can write the running time as

T(N) = 2T(N-1) + O(1) where N is the depth of the triangle.

This will result into a O(2^N) running time.

The space required is almost constant as we are not using extra space, hence we can write it O(1)

The code below explains implementation in java:

## Summary

Above we discussed a recursive solution which is definitely doing a lot of rework, adding memoization to this solution would help. It will further help if we can use dynamic programming and get rid of the recursion at the cost of some extra space. In the next post , we will explore the other two solutions.