Skip to content
Related Articles
Open in App
Not now

Related Articles

Maximum prefix sum after K reversals of a given array

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 29 Jan, 2022
Improve Article
Save Article

Given an array arr[] of size N and a positive integer K, the task is to find the maximum prefix sum after K reversals of the given array. 

Examples:

Input: arr[] = {1, 5, 8, 9, 11, 2}, K = 1
Output: 36
Explanation: Reverse the array once. Therefore, the array becomes {2, 11, 9, 8, 5, 1}. Maximum prefix sum = 2 + 11 + 9 + 8 + 5 + 1 = 36. 

Input: arr[] = {5, 6, -4, 3, -2, -10}, K = 2
Output : 11
Explanation: Reverse the array twice. Therefore, the array becomes {5, 6, -4, 3, -2, -10}. Maximum prefix sum = 5 + 6=11. 

Naive Approach: The simplest approach is to reverse the array K times and after K reversals, find the maximum prefix sum possible by traversing the array and print the maximum sum. 
Time Complexity: O(N * K)
Auxiliary Space: O(1)

Efficient Approach: To optimize the above approach, the idea is based on the observation that if K is odd, then the array gets reversed. Otherwise, the array remains unchanged. Therefore, if K is odd, find the maximum suffix sum. Otherwise, find the maximum prefix sum. Follow the steps below to solve the problem:

  • Initialize sum as INT_MIN to store the required answer.
  • If K is odd, reverse the array arr[].
  • Initialize currSum as 0 to store the prefix sum of elements.
  • Traverse the array arr[] using the variable i and perform the following:
    • Add arr[i] to the variable currSum.
    • If the value of currSum is greater than the sum, then update the sum as currSum.
  • After the above steps, print the value of the sum as the result.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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)
     
    # Print the 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.


Javascript




<script>
 
// Javascript program for the above approach
 
  // Function to find the maximum prefix
  // sum after K reversals of the array
  function maxSumAfterKReverse(arr, K, N)
  {
      
    // Stores the required sum
    let sum = Number.MIN_VALUE;
  
    // If K is odd, reverse the array
    if (K % 2 != 0)
      arr.reverse();
  
    // Store current prefix sum of array
    let currsum = 0;
  
    // Traverse the array arr[]
    for (let 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
    document.write(sum);
  }
 
// Driver Code
 
      let arr = [ 1, 5, 8, 9, 11, 2 ];
    let K = 1;
    let N = arr.length;
  
    // Function Call
    maxSumAfterKReverse(arr, K, N);
  
</script>


  

Output: 

36

 

Time Complexity: O(N)
Auxiliary Space: O(1) 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!