We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

You can't set the initial value of 'total' to 0, because the answer might be lower. To avoid that, set the initial value to the lowest possible (-infinity).

The smallest number allowable for any of the numbers in the hourglass is -9. If all the numbers in an hourglass are -9, the lowest possible value for an hourglass is -63. So an initial value of -63 is ok too.

## 2D Array - DS

You are viewing a single comment's thread. Return to all comments →

Hey awesome solution, just a quick question why use -Infinity

You can't set the initial value of 'total' to 0, because the answer might be lower. To avoid that, set the initial value to the lowest possible (-infinity).

The smallest number allowable for any of the numbers in the hourglass is -9. If all the numbers in an hourglass are -9, the lowest possible value for an hourglass is -63. So an initial value of -63 is ok too.

you can also use -64 since the lowest possible value is -63 :)

I think more general and better solution is to declare maxSum outside of loop and then after calculating sum add:

This sets maxSum to first calculated sum, whatever it is.