


You are given an array A of length N consisting of positive integers. Your task is to print the maximum subsequence sum such that no three consecutive elements are taken from array A.
Note:
A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements
The first line of the input contains ‘T’ denoting the number of test cases.
The first line of each test case contains an integer ‘N’, representing the length of the array.
The second line of each test case contains 'N' space-separated integers of the array 'A'.
Output Format:
For each test case, print a single line containing a single integer denoting the maximum sum subsequence.
The output of each test case will be printed on a new line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10 ^ 4
1 <= A[i] <=10 ^ 5
Where ‘T’ denotes the number of test cases and N denotes the length of array 'A'.
Time limit: 1 sec.
2
3
1 1 1
4
6 3 3 2
2
11
For test case 1:
All subsequences sum are:
1 (A[0]), 1 (A[1]), 1 (A[2]), 2 (A[0] + A[1]), 2 (A[0] + A[2]), 2 (A[1] + A[2]), 3(A[0] + A[1] + A[2])
The max sum subsequence without three consecutive elements is 2 ( A[0] + A[1] or A[0] + a[2] or A[1] + A[2]).
For test case 2:
All subsequences sum are:
6 (A[0]), 3 (A[1]), 3 (A[2]), 2 (A[3]), 9 (A[0] + A[1]), 9 (A[0] + A[2]), 8 (A[0] + A[3]), 6 (A[1] + A[2]), 5 (A[1] + A[3]), 5 * (A[2] + A[3]), … 11 (A[0] + A[1] + A[3]), ... 11 (A[0] + A[2] + A[3])
The max sum subsequence without three consecutive elements is 11 ( A[0] + A[1] + A[3] or A[0] + A[2] + A[3] ).
2
8
1 2 3 4 5 6 7 8
1
46
27
46
Think of exploring all possible combinations under the given conditions.
Explanation:
The key idea is to build our current answer from previous answers, i.e. if we want to calculate the answer from the first ‘i’ elements (0...i), we can use answers from the prefix of i-1, i-2, i-3 elements.
solve(A,i) = max( solve(A,i-1), A[i] + solve(A,i-2), A[i]+A[i-1] +solve(A,i-3) );
To calculate the maximum subsequence sum of the first i elements we have 3 parts:
To make sure we do not use 3 consecutive elements from the array, we deliberately skip some elements.
Algorithm:
O(3 ^ N), where ‘N’ is the length of the array.
As from every position in the array, we are calling 3 more functions in previous positions.
O(N), where ‘N’ is the length of the array.
The depth of the recursion tree will be O(N), thus space O(N) is space occupied by the stack.