Sum of Odd Positions
Python
Medium
3 views
Problem Description
Read n integers, sum the values at odd positions (1st, 3rd, 5th...) and output the sum.
Input Format
First line n. Second line n integers.
Output Format
One integer sum.
Official Solution
import sys
p=sys.stdin.read().strip().split()
if not p: sys.exit(0)
n=int(p[0])
a=list(map(int,p[1:1+n]))
sm=0
for i,v in enumerate(a, start=1):
if i%2==1:
sm+=v
sys.stdout.write(str(sm))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!