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

📅  最后修改于: 2021-05-06 07:23:23             🧑  作者: 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


输出:

13

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