The sum that wraps to negative.
transcript
The search looks right. Java prints a confident answer. The answer is wrong. The sum overflowed.
Two loads, each one and a half billion. One courier takes both, so the answer is three billion. The search starts hi at the sum.
A 32-bit int stops near two billion. Exactly two to the thirty-one, minus one. The sum is three billion. It wraps to a negative number.
Hi is negative, so the loop never runs. It checks nothing at all. It returns one and a half billion. The right answer is three billion.
Use a long for sums. Use a long for hi. Do it for every running total. C plus plus overflow is undefined.
Python integers never overflow. JavaScript is exact up to two to the fifty-three. Neither needs a change here.
Check the size of every sum. A test with huge values would catch it. Full breakdown in the lecture.