

If the ARR is [4,3,11,12,2] ,the zigzag subsequence having maximum sum is [4,3,12,2].
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer, ‘N’, denoting the number of elements in array ‘ARR’.
The following line contains ‘N’ values corresponding to elements of ‘ARR’.
For each test case, print the maximum sum of the zigzag subsequence.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 1000.
1 <=ARR[i] <=10^5.
Time limit: 1 sec
In this approach, we will use recursion and find the answer to the problem. We will define two functions INC(n) and DEC(n).
INC(n) will return the maximum sum if the last element of the subsequence is n’th element and the last value is greater than the previous.
DEC(n) will return the maximum sum if the last element of the subsequence is n’th element and the last value is less than the previous.
Now, we will compute the values using recursion we will iterate through 0 to ‘N’-1th index and find the index for which the sum is maximum.
In the end, we will iterate the whole array and find the maximum value of INC(i) and DEC(i), and return the maximum sum value.
In this approach, we will use the same recursive functions, we used in approach 1 but we will use memoization to reduce the complexity as the answer for each value will be calculated only once.
We will define two arrays ‘INC_DP’ and ‘DEC_DP’ to store the answers.
In this approach, we will use dynamic programming and find the answer to the problem. We will prepare two arrays, INC[] and DEC[], to find the maximum required sum.
INC[i] will store the maximum sum if the last element of the subsequence is i’th element and the last value is greater than the previous.
DEC[i] will store the maximum sum if the last element of the subsequence is i’th element and the last value is less than the previous.
Now, we will compute the values by iterating the array using a nested loop as :
If ‘ARR[i]’ > ‘ARR’[j]: Set ‘DEC[i]’ as maximum of DEC[i] and (ARR[i]+INC[j]) as we can include the ith element.
If ‘ARR[i]’ < ARR[j]: Set ‘INC[i]’ as maximum of INC[i] and (ARR[i]+DEC[j]) as we can include the ith element.
In the end, we will iterate the whole array and find the maximum value of INC[i] and DEC[i], and return the maximum sum value.