📜  通过减去奇数的绝对值并添加偶数元素的绝对值来最大化数组总和

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

通过减去奇数的绝对值并添加偶数元素的绝对值来最大化数组总和

给定一个大小为N的数组arr[] ,任务是通过减去所有奇数的绝对值和添加所有偶数元素的绝对值来最大化数组总和,最多只有一个异常的奇偶对,即只有可以减去一个偶数,可以添加一个奇数。

例子:

方法:可以根据以下思路解决问题:

请按照以下步骤解决问题:

  • 初始化变量 Max、Min、Sum 分别存储最大绝对奇数、最小绝对偶数和总和。
  • 0 到 N – 1遍历数组。
    • 如果元素是奇数,则从 Sum 中减去其绝对值并更新Max
    • 如果元素是偶数,则将其绝对值添加到 Sum 并更新Min。
  • 如果 Min 大于 Max 则不需要更新 sum。
  • 否则,更新 Sum, Sum = Sum + 2*(Max – Min)

下面是上述方法的实现:

C++
// C++ code for above approach
 
#include 
using namespace std;
 
// Function to return maximum sum
// after swapping
int maxSum(int* arr, int N)
{
    // Initialize the variables
    int Max = INT_MIN;
    int Min = INT_MAX;
    int X, Sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        X = arr[i];
 
        // If element is odd then subtract
        // it from the Sum and update maximum
        if (X & 1) {
            Max = max(Max, abs(X));
            Sum -= abs(X);
        }
 
        // Else add it to the Sum and
        // update minimum
        else {
            Min = min(Min, abs(X));
            Sum += abs(X);
        }
    }
 
    // If minimum even element is greater
    // than maximum odd element then no
    // need to swap
    if (Min >= Max)
        return Sum;
 
    // Else print the sum after swapping
    return (Sum + 2 * (Max - Min));
}
 
// Driver Code
int main()
{
    int arr[] = { -5, -4, -3, -2, -1,
                  0, 1, 2, 3, 4, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << maxSum(arr, N);
    return 0;
}


Java
// Java code for above approach
import java.io.*;
 
class GFG
{
   
    // Function to return maximum sum
    // after swapping
    public static int maxSum(int arr[], int N)
    {
       
        // Initialize the variables
        int Max = Integer.MIN_VALUE;
        int Min = Integer.MAX_VALUE;
        int X, Sum = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
            X = arr[i];
 
            // If element is odd then subtract
            // it from the Sum and update maximum
            if ((X & 1) != 0) {
                Max = Math.max(Max, Math.abs(X));
                Sum -= Math.abs(X);
            }
 
            // Else add it to the Sum and
            // update minimum
            else {
                Min = Math.min(Min, Math.abs(X));
                Sum += Math.abs(X);
            }
        }
 
        // If minimum even element is greater
        // than maximum odd element then no
        // need to swap
        if (Min >= Max)
            return Sum;
 
        // Else print the sum after swapping
        return (Sum + (2 * (Max - Min)));
    }
    public static void main(String[] args)
    {
        int arr[]
            = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
        int N = arr.length;
 
        // Function call
        System.out.print(maxSum(arr, N));
    }
}
 
// This code is contributed by Rohit Pradhan.


Python3
# Python code to implement the approach
 
# Function to return maximum sum
# after swapping
import sys
 
def maxSum(arr, N):
   
    # Initialize the variables
    Max = -sys.maxsize -1
    Min = sys.maxsize
    X, Sum = 0,0
 
    # Traverse the array
    for i in range(N):
        X = arr[i]
 
        # If element is odd then subtract
        # it from the Sum and update maximum
        if (X & 1):
            Max = max(Max, abs(X))
            Sum -= abs(X)
 
        # Else add it to the Sum and
        # update minimum
        else:
            Min = min(Min, abs(X))
            Sum += abs(X)
 
    # If minimum even element is greater
    # than maximum odd element then no
    # need to swap
    if (Min >= Max):
        return Sum
 
    # Else prthe sum after swapping
    return (Sum + 2 * (Max - Min))
 
# Driver Code
 
arr = [ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 ]
N = len(arr)
 
# Function call
print(maxSum(arr, N))
 
# This code is contributed by shinjanpatra


C#
// C# code for above approach
using System;
class GFG {
 
    // Function to return maximum sum
    // after swapping
    static int maxSum(int[] arr, int N)
    {
        // Initialize the variables
        int Max = Int32.MinValue;
        int Min = Int32.MaxValue;
        int X, Sum = 0;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
            X = arr[i];
 
            // If element is odd then subtract
            // it from the Sum and update maximum
            if ((X & 1) != 0) {
                Max = Math.Max(Max, Math.Abs(X));
                Sum -= Math.Abs(X);
            }
 
            // Else add it to the Sum and
            // update minimum
            else {
                Min = Math.Min(Min, Math.Abs(X));
                Sum += Math.Abs(X);
            }
        }
 
        // If minimum even element is greater
        // than maximum odd element then no
        // need to swap
        if (Min >= Max)
            return Sum;
 
        // Else print the sum after swapping
        return (Sum + 2 * (Max - Min));
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr
            = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
        int N = arr.Length;
 
        // Function call
        Console.Write(maxSum(arr, N));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
4

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