CHEFALT Alternating Subsequences

Problem: CHEFALT

You are given an array of N non-negative integers: A1A2, …, 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 Ai1Ai2, …, 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.

Solution:

#include <iostream>
using namespace std;

int main(int argc,char const *argv[])
{
    int t;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int sumE=0,sumO=0;
        int n;
        cin>>n;
        for(int j=0;j<n;j++)
        {
            int temp;
            cin>>temp;
            if(j%2==0)
            {
                sumE+=temp;
            }
            else
            {
                sumO+=temp;
            }
        }
        cout<<(sumE>sumO?sumE:sumO)<<endl;
    }
    return 0;
}
The content uploaded on this website is for reference purposes only. Please do it yourself first.