# Burst Balloon to maximize coins

We have been given N balloons, each with a number of coins associated with it. On bursting a balloon i, the number of coins gained is equal to A[i-1]*A[i]*A[i+1]. Also, balloons i-1 and i+1 now become adjacent. Find the maximum possible profit earned after bursting all the balloons. Assume an extra 1 at each boundary.

Examples:

Input : 5, 10 Output : 60 Explanation - First Burst 5, Coins = 1*5*10 Then burst 10, Coins+= 1*10*1 Total = 60 Input : 1, 2, 3, 4, 5 Output : 110

A recursive solution is discussed here. We can solve this problem using dynamic programming.

First, consider a sub-array from indices Left to Right(inclusive).

If we assume the balloon at index Last to be the last balloon to be burst in this sub-array, we would say the coined gained to be-A[left-1]*A[last]*A[right+1].

Also, the total Coin Gained would be this value, plus dp[left][last – 1] + dp[last + 1][right], where dp[i][j] means maximum coin gained for sub-array with indices i, j.

Therefore, for each value of Left and Right, we need find and choose a value of Last with maximum coin gained, and update the dp array.

Our Answer is the value at dp[1][N].

## Python3

`# Python program burst balloon problem. ` ` ` `def` `getMax(A): ` ` ` `N ` `=` `len` `(A) ` ` ` `A ` `=` `[` `1` `] ` `+` `A ` `+` `[` `1` `]` `# Add Bordering Balloons ` ` ` `dp ` `=` `[[` `0` `for` `x ` `in` `range` `(N ` `+` `2` `)] ` `for` `y ` `in` `range` `(N ` `+` `2` `)]` `# Declare DP Array ` ` ` ` ` `for` `length ` `in` `range` `(` `1` `, N ` `+` `1` `): ` ` ` `for` `left ` `in` `range` `(` `1` `, N` `-` `length ` `+` `2` `): ` ` ` `right ` `=` `left ` `+` `length ` `-` `1` ` ` ` ` `# For a sub-array from indices left, right ` ` ` `# This innermost loop finds the last balloon burst ` ` ` `for` `last ` `in` `range` `(left, right ` `+` `1` `): ` ` ` `dp[left][right] ` `=` `max` `(dp[left][right], \ ` ` ` `dp[left][last` `-` `1` `] ` `+` `\ ` ` ` `A[left` `-` `1` `]` `*` `A[last]` `*` `A[right ` `+` `1` `] ` `+` `\ ` ` ` `dp[last ` `+` `1` `][right]) ` ` ` `return` `(dp[` `1` `][N]) ` ` ` `# Driver code ` `A ` `=` `[` `1` `, ` `2` `, ` `3` `, ` `4` `, ` `5` `] ` `print` `(getMax(A)) ` |

*chevron_right*

*filter_none*

**Output:**

110

## Recommended Posts:

- Number of paths with exactly k coins
- Buy minimum items without change and given coins
- Probability of getting at least K heads in N tosses of Coins
- Collect maximum coins before hitting a dead end
- Find minimum number of coins that make a given value
- Probability of getting more heads than tails when N biased coins are tossed
- Probability of getting two consecutive heads after choosing a random coin among two different types of coins
- Maximize arr[j] - arr[i] + arr[l] - arr[k], such that i < j < k < l
- Maximize the value of x + y + z such that ax + by + cz = n
- Maximize the number of subarrays with XOR as zero
- Maximize the bitwise OR of an array
- Remove an element to maximize the GCD of the given array
- Maximize the number of sum pairs which are divisible by K
- Maximize the happiness of the groups on the Trip
- Stock Buy Sell to Maximize Profit

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.