📌  相关文章
📜  给定数组的K个反转后的最大前缀和

📅  最后修改于: 2021-05-17 22:49:11             🧑  作者: Mango

给定大小为N的数组arr []和正整数K ,任务是在给定数组的K个反转之后找到最大前缀和。

例子:

天真的方法:最简单的方法是将数组反转K次,并在K反转之后,通过遍历数组并打印最大和求出最大前缀和。
时间复杂度: O(N * K)
辅助空间: O(1)

高效方法:为了优化上述方法,该思想基于以下观察结果:如果K为奇数,则数组将被反转。否则,数组保持不变。因此,如果K为奇数,请找到最大后缀和。否则,找到最大前缀和。请按照以下步骤解决问题:

  • sum初始化为INT_MIN以存储所需的答案。
  • 如果K为奇数,则反转数组arr []
  • currSum初始化为0以存储元素的前缀和。
  • 使用变量i遍历数组arr []并执行以下操作:
    • arr [i]添加到变量currSum中
    • 如果currSum的值大于sum ,则将总和更新为currSum
  • 完成上述步骤后,打印sum的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum prefix
// sum after K reversals of the array
int maxSumAfterKReverse(int arr[],
                        int K, int N)
{
    // Stores the required sum
    int sum = INT_MIN;
 
    // If K is odd, reverse the array
    if (K & 1)
        reverse(arr, arr + N);
 
    // Store current prefix sum of array
    int currsum = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
 
        // Add arr[i] to currsum
        currsum += arr[i];
 
        // Update maximum prefix sum
        // till now
        sum = max(sum, currsum);
    }
 
    // Print the answer
    cout << sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 5, 8, 9, 11, 2 };
    int K = 1;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    maxSumAfterKReverse(arr, K, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
  // Function to find the maximum prefix
  // sum after K reversals of the array
  static void maxSumAfterKReverse(Integer arr[], int K, int N)
  {
     
    // Stores the required sum
    int sum = Integer.MIN_VALUE;
 
    // If K is odd, reverse the array
    if (K % 2 != 0)
      Collections.reverse(Arrays.asList(arr));
 
    // Store current prefix sum of array
    int currsum = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
      // Add arr[i] to currsum
      currsum += arr[i];
 
      // Update maximum prefix sum
      // till now
      sum = Math.max(sum, currsum);
    }
 
    // Print the answer
    System.out.print(sum);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    Integer[] arr = { 1, 5, 8, 9, 11, 2 };
    int K = 1;
    int N = arr.length;
 
    // Function Call
    maxSumAfterKReverse(arr, K, N);
  }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python3 program for the above approach
import sys
 
# Function to find the maximum prefix
# sum after K reversals of the array
def maxSumAfterKReverse(arr, K, N) :
     
    # Stores the required sum
    sum = -sys.maxsize - 1
 
    # If K is odd, reverse the array
    if (K & 1) :
        arr.reverse()
 
    # Store current prefix sum of array
    currsum = 0
 
    # Traverse the array arr[]
    for i in range(N):
 
        # Add arr[i] to currsum
        currsum += arr[i]
 
        # Update maximum prefix sum
        # till now
        sum = max(sum, currsum)
     
    # Prthe answer
    print(sum)
 
# Driver Code
arr = [ 1, 5, 8, 9, 11, 2 ]
K = 1
N = len(arr)
 
# Function Call
maxSumAfterKReverse(arr, K, N)
 
# This code is contributed by code_hunt.


C#
// C# program for the above approach
using System;
 
class GFG{
 
  // Function to find the maximum prefix
  // sum after K reversals of the array
  static void maxSumAfterKReverse(int[] arr, int K, int N)
  {
 
    // Stores the required sum
    int sum = Int32.MinValue;
 
    // If K is odd, reverse the array
    if (K % 2 != 0)
      Array.Reverse(arr);
 
    // Store current prefix sum of array
    int currsum = 0;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
 
      // Add arr[i] to currsum
      currsum += arr[i];
 
      // Update maximum prefix sum
      // till now
      sum = Math.Max(sum, currsum);
    }
 
    // Print the answer
    Console.Write(sum);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    int[] arr = { 1, 5, 8, 9, 11, 2 };
    int K = 1;
    int N = arr.Length;
 
    // Function Call
    maxSumAfterKReverse(arr, K, N);
  }
}
 
// This code is contributed by sanjoy_62.


输出:
36

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