Sum of Elements in an Even Indexed Array
Easy
Given an array of integers, return the sum of the elements at even indices (i.e., indexes 0, 2, 4, ...).
Example 1
Input
[ 1, 2, 3, 4, 5, 6 ]
Output
9
Explanation. The sum of elements at even indices 0, 2, and 4 is 1 + 3 + 5 = 9.
Example 2
Input
[ 10, 100, 1000, 10000, 100000 ]
Output
110010
Explanation. The sum of elements at even indices 0, 2, and 4 is 10 + 1000 + 100000 = 110010.
Example 3
Input
[ -5, -5, -5, -5 ]
Output
-10
Explanation. The sum of elements at even indices 0 and 2 is -5 + -5 = -10.
Follow-up: How would you optimize your solution if you know that the array could be very large, potentially containing up to 10 million integers?
Constraints:
The array will contain between 1 and 1000 elements, and each element will be an integer between -1000 to 1000.
- Views
- 8