You are given an array of N non-negative integers: A1, A2, ..., AN. An alternating subsequence is a subsequence in which the indices of any two consecutive elements differ by exactly two in the original array. That is, if Ai1, Ai2, ..., Aik is some subsequence, then for it to be an alternating subsequence, (i2 - i1 = 2), (i3 - i2 = 2), and so on should all hold true. Among all alternating subsequences, find the one which has maximum sum of elements, and output that sum.
Input
The first line of the input contains an integer T denoting the number of test cases.
The first line of each test case contains an integer N denoting the number of elements in the array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the array A.
Output
For each test case, output a single line containing the answer.
Note
A subsequence with only a single integer is also an alternating subsequence.
Constraints
1 ≤ T ≤ 10
1 ≤ N ≤ 105
0 ≤ Ai ≤ 105
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
int testcases = 0, size = 0;
printf("%s", "Enter testcases: ");
scanf_s("%d", &testcases);
int most_sum = 0;
for (int i = 0; i != testcases; ++i)
{
printf("%s", "Enter array size: ");
scanf_s("%d", &size);
int* arr = (int*)malloc(size * sizeof(int));
int sum = 0;
for (int j = 0; j != size; ++j)
{
printf("%s", "Enter arr element: ");
scanf_s("%d", &arr[j]);
sum += arr[j];
}
if (sum > most_sum)
{
most_sum = sum;
}
free(arr);
}
printf("%s %d", "Biggest sum is: ", most_sum);
return 0;
Comments
Leave a comment