Subsequence with maximum odd sum

Given a set of integers, check whether there is a subsequence with odd sum and if yes, then finding the maximum odd sum. If no subsequence contains odd sum, return -1.

Examples:

Input : arr[] = {2, 5, -4, 3, -1};
Output : 9
The subsequence with maximum odd 
sum is 2, 5, 3 and -1.

Input : arr[] = {4, -3, 3, -5}
Output : 7
The subsequence with maximum odd
sum is 4 and 3

Input :  arr[] = {2, 4, 6}
Output : -1
There is no subsequence with odd sum.

A simple solution to generate all subsequences and find the maximum sum of all subsequences with odd sums. Time complexity of this solution would be exponential.

An efficient solution can work in O(n) time. The idea is based on following facts.
1) Odd sum is not possible if all numbers are even. Otherwise we always find an answer.
2) If odd sum is possible, we find sum of all positive integers. If sum is odd, we return it as this is maximum overall positive sum. If sum is even, we subtract the odd number with smallest absolute value from sum. This step can be justified by the fact that the smallest absolute odd value number becomes part of result if it negative, and removed from result when it is positive.

// C++ program to find maximum sum of odd 
// subsequence if it exists. 
#include<bits/stdc++.h>
using namespace std;

// Returns maximum sum odd subsequence if exists
// Else returns -1
int findMaxOddSubarraySum(int arr[], int n)
{
    // Here min_odd is the minimum odd number (in
    // absolute terms). Initializing with max value
    // of int .
    int min_odd = INT_MAX;

    // To check if there is al-least one odd number.
    bool isOdd = false;

    int sum = 0;  // To store sum of all positive elements
    for (int i=0 ; i<n ; i++)
    {
        // Adding positive number would increase
        // the sum.
        if (arr[i] > 0)
            sum = sum + arr[i];

        // To find the minimum odd number(absolute)
        // in the array.
        if (arr[i]%2 != 0)
        {
            isOdd = true;
            if (m > abs(arr[i]))
                m = abs(arr[i]);
        }
    }

    // If there was no odd number
    if (isOdd == false)
       return -1;

    // Now, sum will be either odd or even.
    // If even, changing it to odd. As, even - odd = odd.
    // since m is the minimum odd number(absolute).
    if (sum%2 == 0)
        sum = sum - m;

    return sum;
}

// Driver code
int main()
{
    int arr[] = {2, -3, 5, -1, 4};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMaxOddSubarraySum(arr, n);
    return 0;
}

Output:

11

Time Complexity : O(n)
Auxiliary Space : O(1)

This article is contributed by Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.