Subsequence with maximum odd sum
Last Updated :
16 Mar, 2023
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 the following facts.
- Odd sum is not possible if all numbers are even. Otherwise, we always find an answer.
- If odd sum is possible, we find sum of all positive integers. If sum is odd, we return it as this is the maximum overall positive sum. If sum is even, we subtract the odd number with the 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 is negative, and removed from the result when it is positive.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int findMaxOddSubarraySum( int arr[], int n)
{
int min_odd = INT_MAX;
bool isOdd = false ;
int sum = 0;
for ( int i=0 ; i<n ; i++)
{
if (arr[i] > 0)
sum = sum + arr[i];
if (arr[i]%2 != 0)
{
isOdd = true ;
if (min_odd> abs (arr[i]))
min_odd = abs (arr[i]);
}
}
if (isOdd == false )
return -1;
if (sum%2 == 0)
sum = sum - min_odd;
return sum;
}
int main()
{
int arr[] = {2, -3, 5, -1, 4};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << findMaxOddSubarraySum(arr, n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findMaxOddSubarraySum( int arr[], int n)
{
int min_odd = Integer.MAX_VALUE;
boolean isOdd = false ;
int sum = 0 ;
for ( int i = 0 ; i < n ; i++)
{
if (arr[i] > 0 )
sum = sum + arr[i];
if (arr[i] % 2 != 0 )
{
isOdd = true ;
if (min_odd > Math.abs(arr[i]))
min_odd = Math.abs(arr[i]);
}
}
if (isOdd == false )
return - 1 ;
if (sum % 2 == 0 )
sum = sum - min_odd;
return sum;
}
public static void main (String[] args)
{
int arr[] = { 2 , - 3 , 5 , - 1 , 4 };
int n = arr.length;
System.out.println(findMaxOddSubarraySum(arr, n));
}
}
|
Python3
def findMaxOddSubarraySum(arr, n):
min_odd = + 2147483647
isOdd = False
sum = 0
for i in range (n):
if (arr[i] > 0 ):
sum = sum + arr[i]
if (arr[i] % 2 ! = 0 ):
isOdd = True
if (min_odd > abs (arr[i])):
min_odd = abs (arr[i])
if (isOdd = = False ):
return - 1
if ( sum % 2 = = 0 ):
sum = sum - min_odd
return sum
arr = [ 2 , - 3 , 5 , - 1 , 4 ]
n = len (arr)
print (findMaxOddSubarraySum(arr, n))
|
C#
using System;
class GFG {
static int findMaxOddSubarraySum( int []arr, int n)
{
int min_odd = int .MaxValue;
bool isOdd = false ;
int sum = 0;
for ( int i = 0 ; i < n ; i++)
{
if (arr[i] > 0)
sum = sum + arr[i];
if (arr[i] % 2 != 0)
{
isOdd = true ;
if (min_odd > Math.Abs(arr[i]))
min_odd = Math.Abs(arr[i]);
}
}
if (isOdd == false )
return -1;
if (sum % 2 == 0)
sum = sum - min_odd;
return sum;
}
public static void Main ()
{
int []arr = {2, -3, 5, -1, 4};
int n = arr.Length;
Console.Write(findMaxOddSubarraySum(arr, n));
}
}
|
PHP
<?php
function findMaxOddSubarraySum( $arr , $n )
{
$min_odd = PHP_INT_MAX;
$isOdd = false;
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] > 0)
$sum = $sum + $arr [ $i ];
if ( $arr [ $i ] % 2 != 0)
{
$isOdd = true;
if ( $min_odd > abs ( $arr [ $i ]))
$min_odd = abs ( $arr [ $i ]);
}
}
if ( $isOdd == false)
return -1;
if ( $sum % 2 == 0)
$sum = $sum - $min_odd ;
return $sum ;
}
$arr = array (2, -3, 5, -1, 4);
$n = count ( $arr );
echo findMaxOddSubarraySum( $arr , $n );
?>
|
Javascript
<script>
function findMaxOddSubarraySum(arr, n)
{
let min_odd = Number.MAX_VALUE;
let isOdd = false ;
let sum = 0;
for (let i = 0 ; i < n ; i++)
{
if (arr[i] > 0)
sum = sum + arr[i];
if (arr[i] % 2 != 0)
{
isOdd = true ;
if (min_odd > Math.abs(arr[i]))
min_odd = Math.abs(arr[i]);
}
}
if (isOdd == false )
return -1;
if (sum % 2 == 0)
sum = sum - min_odd;
return sum;
}
let arr = [2, -3, 5, -1, 4];
let n = arr.length;
document.write(findMaxOddSubarraySum(arr, n));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...