Sum of Even-Indexed Elements
EasyPractice library · +10 XPprefix sumsarray manipulationbasic programming
Given an array of N integers, compute the sum of all elements that are located at even indices (0, 2, 4, ...). Print this sum.
Input
The first line contains a single integer N (1 <= N <= 1000). The second line contains N space-separated integers a_1, a_2, ..., a_N (-1000 <= a_i <= 1000). The array is 1-indexed in the problem statement, but conceptually 0-indexed in programming for this task.
Output
Print a single integer: the sum of elements at even indices (0, 2, 4, ...).
Constraints
1 <= N <= 1000 -1000 <= a_i <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 10 20 30 40 50
Output
90
Elements at even indices (0, 2, 4) are 10, 30, 50. Their sum is 10 + 30 + 50 = 90.
Example 2
Input
4 -1 2 -3 4
Output
-4
Elements at even indices (0, 2) are -1, -3. Their sum is -1 + (-3) = -4.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 10 20 30 40 50
Example 2 input
4 -1 2 -3 4