📌  相关文章
📜  通过交替相邻元素的符号来最大化数组和

📅  最后修改于: 2021-09-07 02:27:53             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是通过交替相邻数组元素的符号来找到数组元素的最大可能总和。

例子:

方法:该问题可以使用贪心技术解决。这个想法基于这样一个事实,即在交替相邻元素的符号后,数组中负元素的最大计数不能大于1 。请按照以下步骤解决问题:

  • 初始化一个变量,比如MaxAltSum ,通过交替相邻元素的符号来存储数组元素的最大可能总和。
  • 遍历数组并计算数组中负元素的数量。
  • 如果数组中负元素的个数为偶数,则通过交替相邻数组元素的符号可能的最大可能和等于数组元素的绝对值之和,即MaxAltSum = Σabs(arr[i])
  • 否则,通过交替相邻数组元素的符号从数组中获得的最大可能和等于所有可能的数组元素的绝对值之和,但数组元素的最小绝对值除外。即MaxAltSum = ((Σabs(arr[i])) – 2 * X) ,其中X是数组元素的最小绝对值。
  • 最后,打印值MaxAltSum

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to find the maximum sum by alternating
// the signs of adjacent elements of the array
int findMaxSumByAlternatingSign(int arr[], int N)
{
    // Stores count of negative
    // elements in the array
    int cntNeg = 0;
 
    // Stores maximum sum by alternating
    // the signs of adjacent elements
    int MaxAltSum = 0;
 
    // Stores smallest absolute
    // value of array elements
    int SmValue = 0;
 
    // Stores sum of absolute
    // value of array elements
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If arr[i] is
        // a negative number
        if (arr[i] < 0) {
 
            // Update cntNeg
            cntNeg += 1;
        }
 
        // Update sum
        sum += abs(arr[i]);
 
        // Update SmValue
        SmValue = min(SmValue,
                    abs(arr[i]));
    }
 
    // Update MaxAltSum
    MaxAltSum = sum;
 
    // If cntNeg is
    // an odd number
    if (cntNeg & 1) {
 
        // Update MaxAltSum
        MaxAltSum -= 2 * SmValue;
    }
    return MaxAltSum;
}
 
// Drivers Code
int main()
{
 
    int arr[] = { 1, 1, -2, -4, 5 };
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    cout << findMaxSumByAlternatingSign(
        arr, N);
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
 
// Function to find the maximum sum by alternating
// the signs of adjacent elements of the array
static int findMaxSumByAlternatingSign(int arr[],
                                       int N)
{
     
    // Stores count of negative
    // elements in the array
    int cntNeg = 0;
 
    // Stores maximum sum by alternating
    // the signs of adjacent elements
    int MaxAltSum = 0;
 
    // Stores smallest absolute
    // value of array elements
    int SmValue = 0;
 
    // Stores sum of absolute
    // value of array elements
    int sum = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is
        // a negative number
        if (arr[i] < 0)
        {
             
            // Update cntNeg
            cntNeg += 1;
        }
         
        // Update sum
        sum += Math.abs(arr[i]);
         
        // Update SmValue
        SmValue = Math.min(SmValue,
                  Math.abs(arr[i]));
    }
 
    // Update MaxAltSum
    MaxAltSum = sum;
     
    // If cntNeg is
    // an odd number
    if (cntNeg % 2 == 1)
    {
         
        // Update MaxAltSum
        MaxAltSum -= 2 * SmValue;
    }
    return MaxAltSum;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 1, -2, -4, 5 };
    int N = arr.length;
     
    System.out.print(findMaxSumByAlternatingSign(
    arr, N));
}
}
 
// This code is contributed by jana_sayantan


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum sum by
# alternating the signs of adjacent
# elements of the array
def findMaxSumByAlternatingSign(arr, N):
     
    # Stores count of negative
    # elements in the array
    cntNeg = 0
 
    # Stores maximum sum by alternating
    # the signs of adjacent elements
    MaxAltSum = 0
 
    # Stores smallest absolute
    # value of array elements
    SmValue = 0
 
    # Stores sum of absolute
    # value of array elements
    sum = 0
 
    # Traverse the array
    for i in range(N):
         
        # If arr[i] is
        # a negative number
        if (arr[i] < 0):
             
            # Update cntNeg
            cntNeg += 1
 
        # Update sum
        sum += abs(arr[i])
 
        # Update SmValue
        SmValue = min(SmValue, abs(arr[i]))
 
    # Update MaxAltSum
    MaxAltSum = sum
 
    # If cntNeg is
    # an odd number
    if (cntNeg & 1):
         
        # Update MaxAltSum
        MaxAltSum -= 2 * SmValue
         
    return MaxAltSum
 
# Driver Code
if __name__ == '__main__':
     
    arr = [ 1, 1, -2, -4, 5 ]
    N = len(arr)
 
    print(findMaxSumByAlternatingSign(arr, N))
 
# This code is contributed by SURENDRA_GANGWAR


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to find the maximum sum by alternating
// the signs of adjacent elements of the array
static int findMaxSumByAlternatingSign(int []arr,
                                    int N)
{
     
    // Stores count of negative
    // elements in the array
    int cntNeg = 0;
 
    // Stores maximum sum by alternating
    // the signs of adjacent elements
    int MaxAltSum = 0;
 
    // Stores smallest absolute
    // value of array elements
    int SmValue = 0;
 
    // Stores sum of absolute
    // value of array elements
    int sum = 0;
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is
        // a negative number
        if (arr[i] < 0)
        {
             
            // Update cntNeg
            cntNeg += 1;
        }
         
        // Update sum
        sum += Math.Abs(arr[i]);
         
        // Update SmValue
        SmValue = Math.Min(SmValue,
                Math.Abs(arr[i]));
    }
 
    // Update MaxAltSum
    MaxAltSum = sum;
     
    // If cntNeg is
    // an odd number
    if (cntNeg % 2 == 1)
    {
         
        // Update MaxAltSum
        MaxAltSum -= 2 * SmValue;
    }
    return MaxAltSum;
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 1, -2, -4, 5 };
    int N = arr.Length;
     
    Console.Write(findMaxSumByAlternatingSign(
    arr, N));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:

13

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live