


A “Zig-Zag” arrangement is an arrangement where the differences between successive numbers on tokens strictly alternate between positive and negative integers.
“3 8 5 9 4 7 2 6” is a “Zig-Zag” arrangement as the differences “5 -3 4 -5 3 -5 4” alternates between positive and negative.
The first difference (if one exists) may be either positive or negative.
The first line contains a single integer ‘T’ denoting the number of test case
The first line of each test case contains an integer ‘N’ denoting the number of tokens.
The second line of each test case contains ‘N’ space-separated integers denoting the numbers printed on each token.
For each test case, print an integer representing the length of the longest “Zig-Zag” subsequence.
Print the output of each test case in a separate line.
You don’t need to print anything, It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 1000
0 <= X <= 1000
Where ‘X’ denotes the number printed on the token.
Time Limit: 1 sec
We can find every length of the zig-zag arrangement and find the maximum length out of it.
We will use a recursive function, Solve (tokenArray, index, isIncreasing) which will take the array “tokenArray”, index “index”, and a Boolean variable “isIncreasing” which will tell us whether we need to find increasing “Zig-Zag” arrangement or decreasing “Zig-Zag” arrangement.
If the function “Solve” is called after decreasing “Zig-Zag” arrangement then we will call the same function with increasing “Zig-Zag” arrangement and Vice-Versa.
In this approach, we will take a DP matrix of size (N+1 * 2) where ‘N’ is the size of the tokenArray. This DP matrix will store the count of maximum “Zig-Zag” subsequences for increasing (“isIncreasing” = 1) as well as for the decreasing arrangement (“isIncreasing” = 0).
DP [ i ][ 0 ] will store maximum count of subsequence for increasing arrangement and DP [ i ][ 1 ] will store the maximum count of subsequence for decreasing arrangement.
To understand this approach, we will take two arrays for dp named “Dp1” and “Dp2”.
Whenever we pick up any element of the array to be a part of the “Zig-Zag” subsequence, that element could be a part of an increasing “Zig-Zag” arrangement or a decreasing “Zig-Zag” depending upon the previous element.
Similarly, “Dp2[ i ]” refers to the length of the longest “Zig-Zag” subsequence obtained so far considering “ith” element as the last element of the “Zig-Zag” subsequence and ending with a decreasing “Zig-Zag”.
Similarly, “Dp2[ i ]” will be updated.
Our answer will be stored on the (N – 1)th index (where N will be the length of the “tokenArray”) of the dp arrays in such a way that maximum of “Dp1[N-1]” and “Dp2[N-1]” + 1 will be giving us the maximum length of the “Zig-Zag” subsequence.
This approach will have time complexity of O(N ^ 2) and O(N ^ 2 ) as space complexity.
We can further optimise this approach to constant space complexity and linear time complexity:
Since we are dealing with only the current and the previous element of “tokenArray” in the above approach, so instead of taking 2 dp arrays we can make it possible with two variables “increasingElement” and “decreasingElement” which will store the last elements of “tokenArray”.
This problem is equivalent to finding number of alternating maximum and minimum peaks in the “tokenArrray”. If we consider choosing any intermediate number to be a part of the “Zig-Zag” subsequence, the maximum length of “Zig-Zag” subsequence will always be less than or equal to the one obtained by choosing only consecutive maximum and minimum elements.
Zig - Zag subsequence choosing ‘D’ as 3rd point : “A B D E F G”
Zig – Zag subsequence choosing ‘C’ as 3rd point: “A B C G”
From the figure, we can see that, if we choose ‘C’ as a 2nd point in the “Zig-Zag '' subsequence, we can’t include point ‘E’ and ‘F’. Thus we won't be able to obtain the maximum subsequence length.