Best Subsequence

                                  Best Subsequence

 

You are given an array of length N.
- A subsequence is a sequence that can be derived from another sequence by deleting 
some elements without changing the order of the remaining elements.
You know that a subsequence is best if the sum of the elements of the subsequence is 
odd and maximum among all such subsequences.
It is guaranteed that this array contains at least one subsequence with odd sum.
Your task is to find the sum of the elements of the best subsequence.
Input format


- First line of input contains 
denoting number of test cases. 

- First line of each test case contains N, the size of the array.
-
. Second line of each test case contains 
N space-separated integers.

Output format
Print the expected answer for each test case in a new line.

Constraints
1≤T≤1
1≤N≤105
−109≤Ai≤109
Sample Input
2
4
-2 2 1 -3
3
-3 -10 2
Sample Output
3
-1
Explanation
Subsequence for 1st test case is [2,1].
Subsequence for 
2nd test case is [−3,2].
Note: Your code should be able to convert the sample input into the sample output. However, this is not enough to pass the challenge, because the code will be run on multiple test cases. Therefore, your code must solve this problem statement.

Code:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,ele,i,j,k;
    cin>>n;
    int arr[100000];
    int maxe=-100000,maxo=-100000;
    for(i=0;i<n;i++)
    {
        cin>>ele;
        for(j=0;j<ele;j++)
        {
            cin>>arr[j];
            if(arr[j]%2==0)
                {
                    if(maxe<arr[j])
                    maxe=arr[j];
                }
            else
               {
                   if(maxo<arr[j])
                    maxo=arr[j];
               }
           }
        cout<<maxe+maxo<<endl;
        maxe=-10000;
        maxo=-10000;
        
    }
     return 0;
}

 

Please do comment If u have any Queries!
One Comment

Add a Comment

Your email address will not be published. Required fields are marked *