Pick Element By Index
Python
Easy
3 views
Problem Description
Read n integers and an index i (0-based). Output a[i]. If i is outside range or input is invalid, output OUT OF RANGE.
Input Format
Line1 n. Line2 n integers. Line3 i.
Output Format
Value or OUT OF RANGE.
Sample Test Case
Input:
5
10 20 30 40 50
3
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<3:
sys.stdout.write('OUT OF RANGE')
else:
try:
n=int(lines[0].strip())
arr=list(map(int,lines[1].split()[:n]))
i=int(lines[2].strip())
sys.stdout.write(str(arr[i]))
except Exception:
sys.stdout.write('OUT OF RANGE')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!