Find the maximum sum alternating subsequence
Given a sequence of numbers, you have to find the maximum sum alternating subsequence and print the value. A sequence is an alternating sequence when it will be maintain like (increasing) -> (decreasing) ->(increasing) ->(decreasing).
Input:
T Test case
T no. of input string will be given to you.
E.g.
3
2 3 4 8 2 5 6 8
2 3 4 8 2 6 5 4
6 5 9 2 10 77 5
Constrain:
1≤ A[i] ≤50
Output:
Print the value of maximum sum alternating subsequence.
Example
T=3
Input:
2 3 4 8 2 5 6 8
Output:
22 ( 8+6+8)
Input:
2 3 4 8 2 6 5 4
Output:
20 ( 8+ 2+ 6+ 4)
Input:
6 5 9 2 10 77 5
Output:
98 (5+ 9+ 2+ 77+5)
Let N be the number of elements say, X1, X2, X3, ..., Xn
Let f(a) = the value at the index a of the increasing array, and g(a) = the value at the index a of the decreasing array.
To find out the maximum sum alternating sequence we will follow these steps,
f(indexofthecurrentelement) = max
g(indexofthecurrentelement) = max
C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer