Solution to You are given an array of N non-negative integers: A1, A2, ..., AN. An alternating … - Sikademy
Author Image

Archangel Macsika

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

The Answer to the Question
is below this banner.

Can't find a solution anywhere?


Get the Answers Now!

You will get a detailed answer to your question or assignment in the shortest time possible.

Here's the Solution to this Question

#include <iostream>

using namespace std;

int main(void) {

  int N, T, A[10];

  int temp = 0;

  cout << "Enter the number of test cases: ";

  cin >> T;

  for(int x = 0; x < T; x++) {

    cout << "Enter the number of elements in the array:";

    cin >> N;

 cout << "Enter the elements of the array: ";

 for(int i = 0; i < N; i++) {

      cin >> A[i]; 


 int sum = A[N-1] + A[N-2];

    cout << "Sum is: " << sum << endl;


  return 0;


Related Answers

Was this answer helpful?

Join our Community to stay in the know

Get updates for similar and other helpful Answers

Question ID: mtid-3-stid-44-sqid-1009-qpid-6