First Peak Index
Python
Medium
2 views
Problem Description
Read n integers. A peak means a[i] is greater than both neighbours (i-1 and i+1). Output the first peak index (0-based). If no peak, output -1.
Input Format
First line n. Second line n integers.
Output Format
One integer index.
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]))
ans=-1
for i in range(1,n-1):
if a[i]>a[i-1] and a[i]>a[i+1]:
ans=i
break
sys.stdout.write(str(ans))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!