A small business logs its net daily cash change (a gain or a loss) for n days, starting from a balance of exactly 0 before day 1. Given the n daily changes, find the first day (1-indexed) on which the cumulative balance becomes strictly negative.
If the cumulative balance never goes below zero, print -1 instead.
Line 1: an integer n.
Line 2: n space-separated integers, the net change for day 1, day 2, ..., day n.
A single integer: the 1-indexed day on which the cumulative balance first goes strictly below zero, or -1 if that never happens.
Example 1
Input
5 10 -3 -8 5 2
Expected
3
Explanation
Running balance: 10, 7, -1, 4, 6. It first drops below zero on day 3 (balance -1).
Example 2
Input
3 1 2 3
Expected
-1
Explanation
Running balance: 1, 3, 6 - never negative, so the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →