Zigzag Sequence

Given a sequence, find the longest contiguous block in which no three consecutive terms are monotone increasing or monotone decreasing.

Medium5ArrayTwo pointersGreedyImplementationInterviewNo attempts yetTime limit1sMemory limit1024 MB

Problem

A sequence is a zigzag sequence if nowhere in it do three consecutive terms increase monotonically, and nowhere do three consecutive terms decrease monotonically.

More precisely, a sequence AA of length NN is a zigzag sequence if for every ii with 1iN21 \le i \le N-2, neither AiAi+1Ai+2A_i \le A_{i+1} \le A_{i+2} nor AiAi+1Ai+2A_i \ge A_{i+1} \ge A_{i+2} holds.

A sequence AA of length NN is given. Find the maximum length of a contiguous subsequence of AA that is a zigzag sequence.

A sequence BB of length MM is a contiguous subsequence of the sequence AA of length NN when some ii exists with B1=AiB_1 = A_i, B2=Ai+1B_2 = A_{i+1}, ..., BM=Ai+M1B_M = A_{i+M-1}.

Input

The input consists of two lines. The first line contains the length NN of the sequence.

The second line contains NN integers separated by spaces. The ii-th number is AiA_i.

Output

Print the maximum length of a contiguous subsequence of AA that is a zigzag sequence.

Constraints

  • 3N50003 \le N \le 5000
  • 1Ai1091 \le A_i \le 10^9 (1iN1 \le i \le N)