📌  相关文章
📜  通过从数组 [l, r] 中选择一个子段来最大化总和,并将 arr[i] 转换为 (M–arr[i]) 最多一次

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

通过从数组 [l, r] 中选择一个子段来最大化总和,并将 arr[i] 转换为 (M–arr[i]) 最多一次

给定一个由N个正整数和一个正整数M组成的数组arr[] ,任务是在执行最多一次操作后最大化数组的总和。在一个操作中,从数组[l, r]中选择一个子段并将arr[i]转换为M – arr[i]其中l≤i≤r

例子:

朴素方法:解决问题的最简单方法是将操作应用于数组的所有子数组,并找到应用给定操作的最大和。

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

高效方法:上述方法可以通过使用 Kadane 算法进一步优化。请按照以下步骤解决问题:

  • 初始化一个变量,比如 sum 为 0 来存储数组的总和。
  • 使用变量i[0, N-1]范围内迭代并将arr[i]添加到变量sum并将arr [i] 的值修改为M – 2×arr[i]
  • 现在将 Kadane 算法应用于数组arr[]
  • 初始化两个变量,例如mxans0
  • 使用变量i[0, N-1]范围内迭代并执行以下步骤:
    • arr[i]添加到ans
    • 如果ans<0 ,则将ans的值修改为0
    • mx的值修改为max(mx, ans)
  • 打印sum + mx的值作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to maximize the sum after
// performing atmost one operation
int MaxSum(int arr[], int n, int M)
{
    // Variable to store the sum
    int sum = 0;
 
    // Traverse the array and modify arr[]
    for (int i = 0; i < n; i++) {
        sum += arr[i];
        arr[i] = (M - 2 * arr[i]);
    }
 
    int ans = 0, mx = 0;
 
    // Apply Kadane's algorithm
    for (int i = 0; i < n; i++) {
        // Add a[i] to ans
        ans += arr[i];
 
        // If ans<0, modify the value
        // of ans as 0
        if (ans < 0)
            ans = 0;
 
        // Update the value of mx
        mx = max(mx, ans);
    }
 
    // Return the maximum sum
    return mx + sum;
}
 
// Driver Code
int main()
{
    // Given Input
    int arr[] = { 2, 4, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int M = 5;
 
    // Function Call
    cout << MaxSum(arr, N, M);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
   
  // Function to maximize the sum after
// performing atmost one operation
static int MaxSum(int arr[], int n, int M)
{
   
    // Variable to store the sum
    int sum = 0;
 
    // Traverse the array and modify arr[]
    for (int i = 0; i < n; i++) {
        sum += arr[i];
        arr[i] = (M - 2 * arr[i]);
    }
 
    int ans = 0, mx = 0;
 
    // Apply Kadane's algorithm
    for (int i = 0; i < n; i++)
    {
       
        // Add a[i] to ans
        ans += arr[i];
 
        // If ans<0, modify the value
        // of ans as 0
        if (ans < 0)
            ans = 0;
 
        // Update the value of mx
        mx = Math.max(mx, ans);
    }
 
    // Return the maximum sum
    return mx + sum;
}
 
// Driver Code
    public static void main (String[] args) {
       // Given Input
    int arr[] = { 2, 4, 3 };
    int N = arr.length;
    int M = 5;
 
    // Function Call
      
        System.out.println(MaxSum(arr, N, M));
    }
}
 
// This code is contributed by potta lokesh.


Python3
# python 3 program for the above approach
 
# Function to maximize the sum after
# performing atmost one operation
def MaxSum(arr, n, M):
    # Variable to store the sum
    sum = 0
 
    # Traverse the array and modify arr[]
    for i in range(n):
        sum += arr[i]
        arr[i] = (M - 2 * arr[i])
 
    ans = 0
    mx = 0
 
    # Apply Kadane's algorithm
    for i in range(n):
       
        # Add a[i] to ans
        ans += arr[i]
 
        # If ans<0, modify the value
        # of ans as 0
        if (ans < 0):
            ans = 0
 
        # Update the value of mx
        mx = max(mx, ans)
 
    # Return the maximum sum
    return mx + sum
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    arr = [2, 4, 3]
    N = len(arr)
    M = 5
 
    # Function Call
    print(MaxSum(arr, N, M))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to maximize the sum after
    // performing atmost one operation
    static int MaxSum(int[] arr, int n, int M)
    {
 
        // Variable to store the sum
        int sum = 0;
 
        // Traverse the array and modify arr[]
        for (int i = 0; i < n; i++) {
            sum += arr[i];
            arr[i] = (M - 2 * arr[i]);
        }
 
        int ans = 0, mx = 0;
 
        // Apply Kadane's algorithm
        for (int i = 0; i < n; i++) {
 
            // Add a[i] to ans
            ans += arr[i];
 
            // If ans<0, modify the value
            // of ans as 0
            if (ans < 0)
                ans = 0;
 
            // Update the value of mx
            mx = Math.Max(mx, ans);
        }
 
        // Return the maximum sum
        return mx + sum;
    }
 
    // Driver Code
    public static void Main()
    {
        // Given Input
        int[] arr = { 2, 4, 3 };
        int N = arr.Length;
        int M = 5;
 
        // Function Call
 
        Console.WriteLine(MaxSum(arr, N, M));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出
10

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