📌  相关文章
📜  通过删除结束元素来最大化数组的总和

📅  最后修改于: 2021-10-26 02:31:04             🧑  作者: Mango

给定一个大小为N的数组arr ,任务是通过删除末尾元素来最大化数组中剩余元素的总和。
例子:

方法:思路是用贪心算法来解决这个问题。

  1. 首先计算数组的总和。
  2. 然后比较两端的元素,从总和中减去两者中的最小值。这将使剩余总和最大化。
  3. 然后,将剩余的总和添加到结果中。
  4. 重复上述步骤,直到从数组中删除所有元素。然后打印结果总和。

下面是上述方法的实现:

C++
// C++ program to maximize the sum
// of sum of the Array by
// removing end elements
 
#include 
using namespace std;
 
// Function to find
// the maximum sum of sum
int maxRemainingSum(int arr[], int n)
{
    int sum = 0;
 
    // compute the sum of whole array
    for (int i = 0; i < n; i++)
        sum += arr[i];
 
    int i = 0;
    int j = n - 1;
 
    int result = 0;
 
    // Traverse and remove the
    // minimum value from an end
    // to maximum the sum value
    while (i < j) {
 
        // If the left end element
        // is smaller than right end
        if (arr[i] < arr[j]) {
 
            // remove the left end element
            sum -= arr[i];
 
            i++;
        }
 
        // If the right end element
        // is smaller than left end
        else {
 
            // remove the right end element
            sum -= arr[j];
            j--;
        }
 
        // Add the remaining element
        // sum in the result
        result += sum;
    }
 
    // Return the maximum
    // sum of sum
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 1, 7, 2, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << maxRemainingSum(arr, N);
 
    return 0;
}


Java
// Java program to maximize the sum
// of sum of the Array by removing
// end elements
import java.util.*;
 
class GFG{
     
// Function to find
// the maximum sum of sum
static int maxRemainingSum(int arr[], int n)
{
    int sum = 0;
 
    // Compute the sum of whole array
    for(int i = 0; i < n; i++)
        sum += arr[i];
 
    int i = 0;
    int j = n - 1;
    int result = 0;
 
    // Traverse and remove the
    // minimum value from an end
    // to maximum the sum value
    while (i < j)
    {
         
        // If the left end element
        // is smaller than right end
        if (arr[i] < arr[j])
        {
             
            // Remove the left end element
            sum -= arr[i];
            i++;
        }
         
        // If the right end element
        // is smaller than left end
        else
        {
             
            // Remove the right end element
            sum -= arr[j];
            j--;
        }
 
        // Add the remaining element
        // sum in the result
        result += sum;
    }
 
    // Return the maximum
    // sum of sum
    return result;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 3, 1, 7, 2, 1 };
    int N = arr.length;
 
    System.out.println(maxRemainingSum(arr, N));
}
}
 
// This code is contributed by ankitkumar34


Python3
# Python3 program to maximize the
# sum of sum of the Array by
# removing end elements
 
# Function to find the maximum
# sum of sum
def maxRemainingSum(arr, n):
 
    sum = 0
 
    # Compute the sum of whole array
    for i in range(n):
        sum += arr[i]
 
    i = 0
    j = n - 1
 
    result = 0
 
    # Traverse and remove the
    # minimum value from an end
    # to maximum the sum value
    while (i < j):
 
        # If the left end element
        # is smaller than right end
        if (arr[i] < arr[j]):
 
            # Remove the left end element
            sum -= arr[i]
            i += 1
 
        # If the right end element
        # is smaller than left end
        else:
 
            # Remove the right end element
            sum -= arr[j]
            j -= 1
     
        # Add the remaining element
        # sum in the result
        result += sum;
 
    # Return the maximum
    # sum of sum
    return result
 
# Driver code
arr = [ 3, 1, 7, 2, 1 ]
N = len(arr)
 
print(maxRemainingSum(arr, N))
 
# This code is contributed by ankitkumar34


C#
// C# program to maximize the sum
// of sum of the Array by removing
// end elements
using System;
 
class GFG{
     
// Function to find
// the maximum sum of sum
static int maxRemainingSum(int[] arr, int n)
{
    int i, sum = 0;
 
    // Compute the sum of whole array
    for(i = 0; i < n; i++)
        sum += arr[i];
 
    i = 0;
    int j = n - 1;
    int result = 0;
 
    // Traverse and remove the
    // minimum value from an end
    // to maximum the sum value
    while (i < j)
    {
         
        // If the left end element
        // is smaller than right end
        if (arr[i] < arr[j])
        {
             
            // Remove the left end element
            sum -= arr[i];
            i++;
        }
         
        // If the right end element
        // is smaller than left end
        else
        {
             
            // Remove the right end element
            sum -= arr[j];
            j--;
        }
 
        // Add the remaining element
        // sum in the result
        result += sum;
    }
 
    // Return the maximum
    // sum of sum
    return result;
}
 
// Driver code
public static void Main()
{
    int[] arr = { 3, 1, 7, 2, 1 };
    int N = arr.Length;
 
    Console.Write(maxRemainingSum(arr, N));
}
}
 
// This code is contributed by chitranayal


Javascript


输出:
39

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程