All problems

Problem 510

Longest Turbulent Subarray

Medium · 20 pts · Array, Dynamic Programming, Sliding Window

Problem

A block of consecutive elements is turbulent if the comparison between neighbours flips at every step: greater, then smaller, then greater, and so on (or starting with smaller). Equal neighbours break turbulence. Find the length of the longest turbulent block; any single element counts as a turbulent block of length 1. Track the length of the turbulent block ending at each position.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the length of the longest turbulent subarray.

Constraints

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

Example input

9
9 4 2 10 7 8 8 1 9

Example output

5

Explanation

In 9 4 2 10 7 8 8 1 9, the block 4 2 10 7 8 goes down, up, down, up, so it is turbulent with length 5. The two equal 8s break any longer block.

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

Previous: Array Nesting Next: Number of Zero-Filled Subarrays