All problems

Problem 210

Longest Mountain in Array

Medium · 20 pts · Array, Two Pointers

Problem

A mountain is a block of consecutive elements of length at least 3 that strictly increases up to a peak and then strictly decreases. Both the rising and the falling side must have at least one step, and equal neighbouring values break a mountain. Find the length of the longest mountain in the array, or print 0 if there is none. For each position compute how many strictly increasing steps lead up to it from the left and how many strictly decreasing steps follow it to the right. A position with both counts positive is a peak, and its mountain has length up + down + 1.

Input

First line: integer N. Second line: N space-separated integers.

Output

Print the length of the longest mountain.

Constraints

1 <= N <= 100000; 0 <= A[i] <= 10000

Example input

7
2 1 4 7 3 2 5

Example output

5

Explanation

In 2 1 4 7 3 2 5, the block 1 4 7 3 2 rises from 1 to the peak 7 and falls to 2. It has length 5, and no longer mountain exists.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Kth Missing Positive Number Next: Maximum Gap