📜  具有最大奇数的子序列

📅  最后修改于: 2022-05-13 01:57:51.736000             🧑  作者: Mango

具有最大奇数的子序列

给定一组整数,检查是否存在奇和的子序列,如果有,则找到最大奇和。如果没有子序列包含奇数,则返回-1。

例子 :

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.

生成所有子序列并找到所有奇数子序列的最大和的简单解决方案。该解决方案的时间复杂度将是指数级的。

一个有效的解决方案可以在 O(n) 时间内工作。这个想法是基于以下事实。
1)如果所有数字都是偶数,则不可能奇和。否则,我们总能找到答案。
2)如果奇和是可能的,我们找到所有正整数的和。如果 sum 是奇数,我们返回它,因为这是最大的总正和。如果 sum 是偶数,我们从 sum 中减去绝对值最小的奇数。这一步可以通过以下事实来证明:最小的绝对奇数值如果为负数则成为结果的一部分,而当它为正数时从结果中删除。

C++
// C++ program to find maximum sum of odd
// subsequence if it exists.
#include
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 0)
            sum = sum + arr[i];
 
        // To find the minimum odd number(absolute)
        // in the array.
        if (arr[i]%2 != 0)
        {
            isOdd = true;
            if (min_odd> abs(arr[i]))
                min_odd = 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 - min_odd;
 
    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;
}


Java
// Java program to find maximum sum
// of odd subsequence if it exists.
import java.io.*;
 
class GFG {
     
// Returns maximum sum odd subsequence,
// if exists Else returns -1
static 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 = Integer.MAX_VALUE;
 
    // To check if there is al-least
    // one odd number.
    boolean isOdd = false;
     
    // To store sum of all positive elements
    int sum = 0;
    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 (min_odd > Math.abs(arr[i]))
                min_odd = Math.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 - min_odd;
 
    return sum;
}
 
// Driver code
public static void main (String[] args)
{
    int arr[] = {2, -3, 5, -1, 4};
    int n = arr.length;
    System.out.println(findMaxOddSubarraySum(arr, n));
         
}
}
 
// This code is contributed by vt_m


Python3
# Python program to find
# maximum sum of odd
# subsequence if it exists.
 
# Returns maximum sum odd
# subsequence if exists
# Else returns -1
def findMaxOddSubarraySum(arr, n):
 
    # Here min_odd is the
    # minimum odd number (in
    # absolute terms).
    # Initializing with max value
    # of int .
    min_odd = +2147483647
  
    # To check if there is
    # at-least one odd number.
    isOdd = False
     
    # To store sum of
    # all positive elements
    sum = 0 
    for i in range(n):
     
        # 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 (min_odd > abs(arr[i])):
                min_odd = 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
 
arr = [2, -3, 5, -1, 4]
n =len(arr)
 
print(findMaxOddSubarraySum(arr, n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find maximum sum
// of odd subsequence if it exists.
using System;
 
class GFG {
     
    // Returns maximum sum odd subsequence,
    // if exists Else returns -1
    static 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.MaxValue;
     
        // To check if there is al-least
        // one odd number.
        bool isOdd = false;
         
        // To store sum of all positive elements
        int sum = 0;
        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 (min_odd > Math.Abs(arr[i]))
                    min_odd = Math.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 - min_odd;
     
        return sum;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr = {2, -3, 5, -1, 4};
        int n = arr.Length;
        Console.Write(findMaxOddSubarraySum(arr, n));
             
    }
}
 
// This code is contributed by nitin mittal.


PHP
 0)
            $sum = $sum + $arr[$i];
 
        // To find the minimum odd
        // number(absolute) in the array.
        if ($arr[$i] % 2 != 0)
        {
            $isOdd = true;
            if ($min_odd > abs($arr[$i]))
                $min_odd = 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 - $min_odd;
 
    return $sum;
}
 
// Driver code
$arr = array(2, -3, 5, -1, 4);
$n = count($arr);
echo findMaxOddSubarraySum($arr, $n);
 
// This code is contributed by anuj_67.
?>


Javascript


输出 :

11

时间复杂度:O(n)
辅助空间:O(1)