Skip to main content
๐Ÿ“œ WAYPOINT LESSON

Overflow Hunting

โญโญโญ advancedโณ 30 min read๐Ÿ“ Lesson 185 of 281

2e9 + 2e9 is negative if your sum is int. Where does the standard say values can go, and where does your type stop?

Know your type limits

int stops at 2147483647; a[i]+a[j] with a_i near 2e9 needs long long BEFORE the addition. Two-pointer pair-sums silently walk off when the sum wraps negative. Read constraints like a compiler: every maximum multiplied, every sum accumulated โ€” does the widest expression fit?