Zigzag Sequence
InterviewTime limit1sMemory limit1024 MB
Given a sequence, find the longest contiguous block in which no three consecutive terms are monotone increasing or monotone decreasing.
- Level
Medium5 of 10
- Topics
- Array, Two pointers, Greedy, Implementation
- Solved
- No attempts yet
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 of length is a zigzag sequence if for every with , neither nor holds.
A sequence of length is given. Find the maximum length of a contiguous subsequence of that is a zigzag sequence.
A sequence of length is a contiguous subsequence of the sequence of length when some exists with , , ..., .
Input
The input consists of two lines. The first line contains the length of the sequence.
The second line contains integers separated by spaces. The -th number is .
Output
Print the maximum length of a contiguous subsequence of that is a zigzag sequence.
Constraints
- ()