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
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution