📌  相关文章
📜  合并数组中的所有元素后的最大值

📅  最后修改于: 2021-04-26 05:16:41             🧑  作者: Mango

给定大小为N的数组a 。任务是合并数组中的所有元素并找到最大可能值。如下所述,可以合并数组中的两个元素。

例子:

方法:

  • 如果数组同时包含正负元素,则将数组中所有元素的绝对值相加
  • 如果数组仅包含肯定元素。然后从所有其他元素的总和中减去最小元素
  • 如果数组仅包含否定元素。首先,将所有元素替换为其绝对值。然后从所有其他元素的总和中减去最小元素

下面是上述方法的实现:

C++
// CPP program to maximum value after 
// merging all elements in the array
#include 
using namespace std;
  
// Function to maximum value after 
// merging all elements in the array
int Max_sum(int a[], int n)
{
    // To check if positive and negative 
    // elements present or not
    int pos = 0, neg = 0;
      
    for(int i = 0; i < n; i++)
    {
        // Check for positive integer
        if(a[i] > 0)
            pos = 1;
              
        // Check for negative integer
        else if(a[i] < 0)
            neg = 1;
              
        // If both positive and negative
        // elements are present
        if(pos == 1 and neg == 1)
            break;
    }
      
    // To store maximum value possible
    int sum = 0;
      
    if(pos==1 and neg==1)
    {
        for(int i=0; i < n ; i++)
            sum += abs(a[i]);
    }
      
    else if(pos == 1)
    {
        // To find minimum value
        int mini = a[0];
        sum = a[0];
        for(int i=1; i < n; i++)
        {
            mini = min(mini, a[i]);
            sum += a[i];
        }    
          
        // Remove minimum element
        sum -= 2*mini;
    }    
      
    else if(neg == 1)
    {
        // Replace with absolute values
        for(int i = 0; i < n; i++)
            a[i] = abs(a[i]);
              
        // To find minimum value
        int mini = a[0];
        sum = a[0];
        for(int i=1; i < n; i++)
        {
            mini = min(mini, a[i]);
            sum += a[i];
        }    
          
        // Remove minimum element
        sum -= 2*mini;
          
    }
      
    // Return the required sum
    return sum;
}
  
// Driver code
int main()
{
    int a[] = {1, 3, 5, -2, -6};
      
    int n = sizeof(a) / sizeof(a[0]);
      
    // Function call
    cout << Max_sum(a, n);
      
    return 0;
}


Java
// Java program to maximum value after 
// merging all elements in the array
import java.io.*;
  
class GFG 
{
      
// Function to maximum value after 
// merging all elements in the array
static int Max_sum(int a[], int n)
{
    // To check if positive and negative 
    // elements present or not
    int pos = 0, neg = 0;
      
    for(int i = 0; i < n; i++)
    {
        // Check for positive integer
        if(a[i] > 0)
            pos = 1;
              
        // Check for negative integer
        else if(a[i] < 0)
            neg = 1;
              
        // If both positive and negative
        // elements are present
        if((pos == 1) && (neg == 1))
            break;
    }
      
    // To store maximum value possible
    int sum = 0;
      
    if((pos == 1) && (neg == 1))
    {
        for(int i = 0; i < n ; i++)
            sum += Math.abs(a[i]);
    }
      
    else if(pos == 1)
    {
        // To find minimum value
        int mini = a[0];
        sum = a[0];
        for(int i = 1; i < n; i++)
        {
            mini = Math.min(mini, a[i]);
            sum += a[i];
        } 
          
        // Remove minimum element
        sum -= 2*mini;
    } 
      
    else if(neg == 1)
    {
        // Replace with absolute values
        for(int i = 0; i < n; i++)
            a[i] = Math.abs(a[i]);
              
        // To find minimum value
        int mini = a[0];
        sum = a[0];
        for(int i = 1; i < n; i++)
        {
            mini = Math.min(mini, a[i]);
            sum += a[i];
        } 
          
        // Remove minimum element
        sum -= 2*mini;
          
    }
      
    // Return the required sum
    return sum;
}
  
// Driver code
public static void main (String[] args)
{
  
    int []a = {1, 3, 5, -2, -6};
    int n = a.length;
    // Function call
    System.out.println (Max_sum(a, n));
}
}
  
// This code is contributed by ajit.


Python3
# Python 3 program to maximum value after 
# merging all elements in the array
  
# Function to maximum value after 
# merging all elements in the array
def Max_sum(a, n):
    # To check if positive and negative 
    # elements present or not
    pos = 0
    neg = 0
      
    for i in range(n):
        # Check for positive integer
        if(a[i] > 0):
            pos = 1
              
        # Check for negative integer
        elif(a[i] < 0):
            neg = 1
              
        # If both positive and negative
        # elements are present
        if(pos == 1 and neg == 1):
            break
      
    # To store maximum value possible
    sum = 0
      
    if(pos==1 and neg==1):
        for i in range(n):
            sum += abs(a[i])
      
    elif(pos == 1):
        # To find minimum value
        mini = a[0]
        sum = a[0]
        for i in range(1,n,1):
            mini = min(mini, a[i])
            sum += a[i] 
          
        # Remove minimum element
        sum -= 2*mini
      
    elif(neg == 1):
        # Replace with absolute values
        for i in range(n):
            a[i] = abs(a[i])
              
        # To find minimum value
        mini = a[0]
        sum = a[0]
        for i in range(1,n):
            mini = min(mini, a[i])
            sum += a[i] 
          
        # Remove minimum element
        sum -= 2*mini
             
    # Return the required sum
    return sum
  
# Driver code
if __name__ == '__main__':
    a = [1, 3, 5, -2, -6]
      
    n = len(a)
      
    # Function call
    print(Max_sum(a, n))
  
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to maximum value after 
// merging all elements in the array 
using System;
  
class GFG
{
  
    // Function to maximum value after 
    // merging all elements in the array 
    static int Max_sum(int[] a, int n)
    {
        // To check if positive and negative 
        // elements present or not 
        int pos = 0, neg = 0;
  
        for (int i = 0; i < n; i++)
        {
            // Check for positive integer 
            if (a[i] > 0)
                pos = 1;
  
            // Check for negative integer 
            else if (a[i] < 0)
                neg = 1;
  
            // If both positive and negative 
            // elements are present 
            if ((pos == 1) && (neg == 1))
                break;
        }
  
        // To store maximum value possible 
        int sum = 0;
  
        if ((pos == 1) && (neg == 1))
        {
            for (int i = 0; i < n; i++)
                sum += Math.Abs(a[i]);
        }
  
        else if (pos == 1)
        {
            // To find minimum value 
            int mini = a[0];
            sum = a[0];
            for (int i = 1; i < n; i++)
            {
                mini = Math.Min(mini, a[i]);
                sum += a[i];
            }
  
            // Remove minimum element 
            sum -= 2 * mini;
        }
  
        else if (neg == 1)
        {
            // Replace with absolute values 
            for (int i = 0; i < n; i++)
                a[i] = Math.Abs(a[i]);
  
            // To find minimum value 
            int mini = a[0];
            sum = a[0];
            for (int i = 1; i < n; i++)
            {
                mini = Math.Min(mini, a[i]);
                sum += a[i];
            }
  
            // Remove minimum element 
            sum -= 2 * mini;
  
        }
  
        // Return the required sum 
        return sum;
    }
  
    // Driver code 
    public static void Main(String[] args)
    {
  
        int[] a = { 1, 3, 5, -2, -6 };
        int n = a.Length;
          
        // Function call 
        Console.WriteLine(Max_sum(a, n));
    }
}
  
// This code is contributed by
// sanjeev2552


输出:

17