📜  通过在 K 个不同位置精确添加 S 得到的最大和连续子数组

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

通过在 K 个不同位置精确添加 S 得到的最大和连续子数组

给定一个长度为N的数组arr[] ,任务是通过对[0, N]中的每个K在数组中的K个不同位置添加一个整数S来找到最大和的连续子数组。

例子:

方法:该方法基于前缀和技术

按照以下步骤解决上述问题:

  • 初始化数组prefixsum[]存储数组的前缀和,数组msum[]存储大小为i的子数组和的最大值。
  • 现在遍历数组并计算数组的前缀和。
  • 使用两个 for 循环计算每个大小 i 的最大子数组和并存储在 msum[i] 中。
  • 现在,对于 [0, n] 中的每个 k,在 k 个不同位置添加 s,以最大化大小为 i 的子数组总和。
  • 打印每个 k 的最大子数组和。

下面是上述方法的实现:

C++
// C++ program for largest sum
// contiguous subarray by adding S
// exactly at K different positions
 
#include 
using namespace std;
 
// Function to find the largest sum
// subarray after adding s at k
// different positions for k from [0, n]
void find_maxsum_subarray(
    int arr[], int n, int s)
{
    int msum[n];
    int prefix_sum[n + 1] = { 0 };
    prefix_sum[0] = 0;
 
    // Find the prefix sum
    for (int i = 0; i < n; i++) {
        if (i == 0)
            prefix_sum[i + 1] = arr[i];
        else
            prefix_sum[i + 1]
                = arr[i]
                  + prefix_sum[i];
    }
 
    // For each subarray of size i
    // find the maximum sum
    for (int i = 0; i < n; i++) {
 
        int mx_sum = INT_MIN;
 
        // Check for every subarray of size i
        for (int j = 0; j < n - i; j++) {
            mx_sum
                = max(mx_sum,
                      prefix_sum[j + i + 1]
                          - prefix_sum[j]);
        }
 
        // Store the maximum sub array sum for
        // each subarray of size i in msum array
        msum[i] = mx_sum;
    }
 
    // For every k check the max sum
    // subarray by adding s
    // at k different positions
    for (int k = 0; k <= n; k++) {
 
        int mx_sum = 0;
 
        // For each maxsum of subarray of size i
        // check by s at k positions
        // find the maximum sum
        // after adding s at k positions
        for (int i = 0; i < n; i++) {
 
            mx_sum
                = max(mx_sum,
                      msum[i]
                          + min(i + 1, k) * s);
        }
 
        // For each k
        // print the maximum subarray sum
        cout << mx_sum << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 4, 1, 3, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int s = 2;
    find_maxsum_subarray(arr, n, s);
}


Java
// Java program for largest sum
// contiguous subarray by adding S
// exactly at K different positions
import java.io.*;
 
class GFG {
 
  // Function to find the largest sum
  // subarray after adding s at k
  // different positions for k from [0, n]
  static void find_maxsum_subarray(
    int []arr, int n, int s)
  {
    int []msum = new int[n];
 
    int []prefix_sum = new int[n + 1];
    for(int i = 0; i < n + 1; i++) {
      prefix_sum[i] = 0;
    }
 
    prefix_sum[0] = 0;
 
    // Find the prefix sum
    for (int i = 0; i < n; i++) {
      if (i == 0)
        prefix_sum[i + 1] = arr[i];
      else
        prefix_sum[i + 1]
        = arr[i]
        + prefix_sum[i];
    }
 
    // For each subarray of size i
    // find the maximum sum
    for (int i = 0; i < n; i++) {
 
      int mx_sum = Integer.MIN_VALUE;
 
      // Check for every subarray of size i
      for (int j = 0; j < n - i; j++) {
        mx_sum
          = Math.max(mx_sum,
                     prefix_sum[j + i + 1]
                     - prefix_sum[j]);
      }
 
      // Store the maximum sub array sum for
      // each subarray of size i in msum array
      msum[i] = mx_sum;
    }
 
    // For every k check the max sum
    // subarray by adding s
    // at k different positions
    for (int k = 0; k <= n; k++) {
 
      int mx_sum = 0;
 
      // For each maxsum of subarray of size i
      // check by s at k positions
      // find the maximum sum
      // after adding s at k positions
      for (int i = 0; i < n; i++) {
 
        mx_sum
          = Math.max(mx_sum,
                     msum[i]
                     + Math.min(i + 1, k) * s);
      }
 
      // For each k
      // print the maximum subarray sum
      System.out.print(mx_sum + " ");
    }
  }
 
  // Driver code
  public static void main (String[] args) {
    int []arr = { 4, 1, 3, 2 };
    int n = arr.length;
    int s = 2;
    find_maxsum_subarray(arr, n, s);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python3 program for largest sum
# contiguous subarray by adding S
# exactly at K different positions
 
# Function to find the largest sum
# subarray after adding s at k
# different positions for k from [0, n]
import sys
 
def find_maxsum_subarray(arr, n, s):
    msum = [0]*n
    prefix_sum = [0]*(n+1)
 
    # Find the prefix sum
    for i in range(n):
        if (i == 0):
            prefix_sum[i + 1] = arr[i]
        else:
            prefix_sum[i + 1] = arr[i] + prefix_sum[i]
 
    # For each subarray of size i
    # find the maximum sum
    for i in range(n):
 
        mx_sum = -sys.maxsize-1
 
        # Check for every subarray of size i
        for j in range(n - i):
            mx_sum = max(mx_sum,prefix_sum[j + i + 1]-prefix_sum[j])
 
        # Store the maximum sub array sum for
        # each subarray of size i in msum array
        msum[i] = mx_sum
 
    # For every k check the max sum
    # subarray by adding s
    # at k different positions
    for k in range(n+1):
 
        mx_sum = 0
 
        # For each maxsum of subarray of size i
        # check by s at k positions
        # find the maximum sum
        # after adding s at k positions
        for i in range(n):
            mx_sum = max(mx_sum,msum[i]+ min(i + 1, k) * s)
 
        # For each k
        # print the maximum subarray sum
        print(mx_sum,end=" ")
 
# Driver code
arr = [ 4, 1, 3, 2 ]
n = len(arr)
s = 2
find_maxsum_subarray(arr, n, s)
 
# This code is contributed by shinjanpatra


C#
// C# program for largest sum
// contiguous subarray by adding S
// exactly at K different positions
using System;
class GFG
{
 
  // Function to find the largest sum
  // subarray after adding s at k
  // different positions for k from [0, n]
  static void find_maxsum_subarray(
    int []arr, int n, int s)
  {
    int []msum = new int[n];
 
    int []prefix_sum = new int[n + 1];
    for(int i = 0; i < n + 1; i++) {
      prefix_sum[i] = 0;
    }
 
    prefix_sum[0] = 0;
 
    // Find the prefix sum
    for (int i = 0; i < n; i++) {
      if (i == 0)
        prefix_sum[i + 1] = arr[i];
      else
        prefix_sum[i + 1]
        = arr[i]
        + prefix_sum[i];
    }
 
    // For each subarray of size i
    // find the maximum sum
    for (int i = 0; i < n; i++) {
 
      int mx_sum = Int32.MinValue;
 
      // Check for every subarray of size i
      for (int j = 0; j < n - i; j++) {
        mx_sum
          = Math.Max(mx_sum,
                     prefix_sum[j + i + 1]
                     - prefix_sum[j]);
      }
 
      // Store the maximum sub array sum for
      // each subarray of size i in msum array
      msum[i] = mx_sum;
    }
 
    // For every k check the max sum
    // subarray by adding s
    // at k different positions
    for (int k = 0; k <= n; k++) {
 
      int mx_sum = 0;
 
      // For each maxsum of subarray of size i
      // check by s at k positions
      // find the maximum sum
      // after adding s at k positions
      for (int i = 0; i < n; i++) {
 
        mx_sum
          = Math.Max(mx_sum,
                     msum[i]
                     + Math.Min(i + 1, k) * s);
      }
 
      // For each k
      // print the maximum subarray sum
      Console.Write(mx_sum + " ");
    }
  }
 
  // Driver code
  public static void Main()
  {
    int []arr = { 4, 1, 3, 2 };
    int n = arr.Length;
    int s = 2;
    find_maxsum_subarray(arr, n, s);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
10 12 14 16 18 

时间复杂度: O(N^2) 其中 N 是数组的大小。
辅助空间: O(N)