forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternatingSubsequences.cpp
More file actions
26 lines (26 loc) · 906 Bytes
/
Copy pathAlternatingSubsequences.cpp
File metadata and controls
26 lines (26 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*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.*/
#include <iostream>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
long long int n,pd=0,curr=0,max=0;
cin>>n;
long long int arr[n];
for(long long int i=0;i<n;i++)
{
cin>>arr[i];
if(i%2==0)
curr+=arr[i];
else pd+=arr[i];
}
max=pd>curr?pd:curr;
cout<<max<<endl;
}
return 0;
}