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 A of length N is a zigzag sequence if for every i with 1≤i≤N−2, neither Ai≤Ai+1≤Ai+2 nor Ai≥Ai+1≥Ai+2 holds.
A sequence A of length N is given. Find the maximum length of a contiguous subsequence of A that is a zigzag sequence.
A sequence B of length M is a contiguous subsequence of the sequence A of length N when some i exists with B1=Ai, B2=Ai+1, ..., BM=Ai+M−1.