A total that wraps
transcript
Three builds. A billion seconds each. Add them up.
In thirty two bits the sum wraps. You get a negative number. In sixty four bits it is exact. The first attempt used a plain integer. The sum wraps round. Wide totals cost nothing. Small tests hide overflow.
A thirty two bit integer stops at two point one billion. The sum is three billion. It is over the limit. It worked on small tests. Count times the largest value. Narrow totals cost a wrong answer. Large tests show it.
Multiply the count by the largest value. Above two point one billion, use sixty four bits. It failed on the largest input. That product decides the width. Pick the wide type early.
Check from the constraints. Check before you code. Java does not warn you. Check it before you add. Then test the largest input.
Check the width before you add. Full breakdown in the lecture.