📜  最大偶数和子序列

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

最大偶数和子序列

给定一个由 n 个正整数和负整数组成的数组,找到具有最大偶数和的子序列并显示该偶数和。
例子:

Input: arr[] = {-2, 2, -3, 1, 3}
Output: 6
Explanation: The longest subsequence
with even sum is 2, 1 and 3.

Input: arr[] = {-2, 2, -3, 4, 5}
Output: 8
Explanation: The longest subsequence
with even sum is 2,-3,4 and 5

解决问题的方法可以简化为以下几点:
1)总结所有正数
2)如果总和是偶数,那么这将是可能的最大总和
3)如果总和不是偶数,则从中减去正奇数,或添加负奇数。
— 求负奇数的最大最大奇数,因此 sum+a[I](因为 a[I] 本身是负数)
— 求正奇数的最小最小奇数,因此是 sun-a[I]。
——两个结果中的最大值就是答案。
下面是上述方法的实现

C++
// CPP program to find longest even sum
// subsequence.
#include 
using namespace std;
 
// Returns sum of maximum even sum subsequence
int maxEvenSum(int arr[], int n)
{
    // Find sum of positive numbers
    int pos_sum = 0;
    for (int i = 0; i < n; ++i)
        if (arr[i] > 0)
            pos_sum += arr[i];
 
    // If sum is even, it is our
    // answer
    if (pos_sum % 2 == 0)
        return pos_sum;
 
    // Traverse the array to find the
    // maximum sum by adding a positive
    // odd or subtracting a negative odd
    int ans = INT_MIN;
    for (int i = 0; i < n; ++i) {
        if (arr[i] % 2 != 0) {
            if (arr[i] > 0)
                ans = max(ans, pos_sum - arr[i]);
            else
                ans = max(ans, pos_sum + arr[i]);
        }
    }
 
    return ans;
}
 
// driver program
int main()
{
    int a[] = { -2, 2, -3, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << maxEvenSum(a, n);
    return 0;
}


Java
// Java program to find longest
// even sum subsequence.
class MaxevenSum
{
     
    // Returns sum of maximum even sum
    // subsequence
    static int maxEvenSum(int arr[], int n)
    {
        // Find sum of positive numbers
        int pos_sum = 0;
        for (int i = 0; i < n; ++i)
            if (arr[i] > 0)
                pos_sum += arr[i];
 
        // If sum is even, it is our
        // answer
        if (pos_sum % 2 == 0)
            return pos_sum;
 
        // Traverse the array to find the
        // maximum sum by adding a
        // positive odd or subtracting a
        // negative odd
        int ans = Integer.MIN_VALUE;
        for (int i = 0; i < n; ++i) {
            if (arr[i] % 2 != 0) {
                if (arr[i] > 0)
                    ans = ans>(pos_sum - arr[i]) ?
                          ans:(pos_sum - arr[i]);
                else
                    ans = ans>(pos_sum + arr[i]) ?
                          ans:(pos_sum + arr[i]);
            }
        }  
 
        return ans;
    }
 
    // driver program  
    public static void main(String s[])
    {
        int a[] = {-2, 2, -3, 1};
         
        System.out.println(maxEvenSum(a, a.length));   
 
    }
}
// This code is contributed by Prerna Saini


Python3
# Python 3 program to find longest
# even sum subsequence.
INT_MIN = -100000000
 
# Returns sum of maximum even
# sum subsequence
def maxEvenSum(arr, n):
     
    # Find sum of positive numbers
    pos_sum = 0
    for i in range(n):
        if (arr[i] > 0):
            pos_sum += arr[i]
 
    # If sum is even, it is our answer
    if (pos_sum % 2 == 0):
        return pos_sum
 
    # Traverse the array to find the
    # maximum sum by adding a positive
    # odd or subtracting a negative odd
    ans = INT_MIN;
    for i in range(n):
        if (arr[i] % 2 != 0):
            if (arr[i] > 0):
                ans = max(ans, pos_sum - arr[i])
            else:
                ans = max(ans, pos_sum + arr[i])
    return ans
 
# Driver Code
a = [-2, 2, -3, 1]
n = len(a)
print(maxEvenSum(a, n))
 
# This code is contributed by sahilshelangia


C#
// C# program to find longest
// even sum subsequence.
using System;
 
class GFG {
     
    // Returns sum of maximum even sum
    // subsequence
    static int maxEvenSum(int []arr, int n)
    {
         
        // Find sum of positive numbers
        int pos_sum = 0;
        for (int i = 0; i < n; ++i)
            if (arr[i] > 0)
                pos_sum += arr[i];
 
        // If sum is even, it is our
        // answer
        if (pos_sum % 2 == 0)
            return pos_sum;
 
        // Traverse the array to find the
        // maximum sum by adding a
        // positive odd or subtracting a
        // negative odd
        int ans = int.MinValue;
         
        for (int i = 0; i < n; ++i)
        {
            if (arr[i] % 2 != 0)
            {
                if (arr[i] > 0)
                    ans = (ans > (pos_sum - arr[i]))
                         ? ans : (pos_sum - arr[i]);
                else
                    ans = (ans > (pos_sum + arr[i]))
                         ? ans : (pos_sum + arr[i]);
            }
        }
 
        return ans;
    }
 
    // driver program
    public static void Main()
    {
        int []a = {-2, 2, -3, 1};
         
        Console.WriteLine(maxEvenSum(a, a.Length));
 
    }
}
 
// This code is contributed by vt_m.


PHP
 0)
            $pos_sum += $arr[$i];
 
    // If sum is even, it is our
    // answer
    if ($pos_sum % 2 == 0)
        return $pos_sum;
 
    // Traverse the array to find
    // the maximum sum by adding
    // a positive odd or
    // subtracting a negative odd
    $ans = PHP_INT_MIN;
    for ( $i = 0; $i < $n; ++$i) {
        if ($arr[$i] % 2 != 0) {
            if ($arr[$i] > 0)
                $ans = max($ans,
                 $pos_sum - $arr[$i]);
            else
                $ans = max($ans,
                 $pos_sum + $arr[$i]);
        }
    }
 
    return $ans;
}
 
// driver program
    $a = array( -2, 2, -3, 1 );
    $n = count($a);
    echo maxEvenSum($a, $n);
 
// This code is contributed by anuj_67.
?>


Javascript


输出:

2