Breaking

Saturday, 30 September 2017

Best Subsequence



                                  Best Subsequence






You are given an array of length [Math Processing Error]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 
[Math Processing Error]Tdenoting number of test cases. 
- First line of each test case contains [Math Processing Error]N, the size of the array.
- Second line of each test case contains 
[Math Processing Error]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 [Math Processing Error]1st test case is [Math Processing Error][2,1].
Subsequence for 
[Math Processing Error]2nd test case is [Math Processing Error][−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!

1 comment:

  1. Whoever have made this website and put up all this effort to share the codes... I am Truely Thank ful to you...

    ReplyDelete

Like