Sum of Elements
easySave
ArrayMath
Given an array of integers, compute the sum of all the elements in the array.
Example 1
Input
[ 1, 2, 3, 4 ]
Output
10
Explanation. The sum of 1+2+3+4 is 10.
Example 2
Input
[ 5, -3, 7, -2 ]
Output
7
Explanation. The sum of 5 + (-3) + 7 + (-2) is 7.
Example 3
Input
[ -1, -1, -1, -1 ]
Output
-4
Explanation. The sum of -1 + (-1) + (-1) + (-1) is -4.
Follow-up: Can you solve the problem in linear time and constant space?
Constraints:
The input array will not be empty and will contain at least one integer.
- Views
- 3