210. Sum an Array
Foundation1000 ms256 MBSolved by 0%
Print the sum of n integers.
Think of it recursively: the sum of an array is the first element plus the sum of everything after it, and the sum of an empty array is 0.
That empty case is the base case, and it is worth noticing that the natural answer there is 0 rather than "there is no answer".
Constraints - `1 ≤ n ≤ 100000` - `-1000000 ≤ a[i] ≤ 1000000`
Input
The first line contains an integer n.
The second line contains n integers separated by spaces.
Output
Print one integer, the sum of the n values.
Input5
1 2 3 4 5
Output15
Noteis a small positive case
Input1
7
Output7
Noteis a single element, one step from the base case
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.