GOLDMANSACHS Coding Question β Solved
A traveler is traveling from the city of Zeta to Omega. He starts with X amount of money. Every day he spends some money and may also work on certain days to earn money. On some days, he may earn more than he spends, and on other days, he may spend more than he earns.
You are given an array of integers, which represents his net savings (earnings - expenses) on any day. Your task is to find out the minimum amount the traveler should begin with to ensure that he always has some money (> 0) at the end of any day.
Constraints:
- -200 <= a <= 200, where `a` are the elements of the array.
- 0 < i <= 100, where `i` is the array length.
- X >= 0
Example:
Input:
3 // Array length
4 // Array elements start
2
-3
Output:
0
Explanation:
Traveler saves $4 on the first day, $2 on the second day, and $-3 on the third day (expenses are more on the third day than earnings).
- End of the first day, he has X + $4.
- End of the second day, he has X + $(4 + 2).
- End of the third day, he has X + $(4 + 2 - 3).
So, effectively, the traveler can start with X = $0 to always ensure he has some money at the end of each day.