Open In App

Maximize Array sum by replacing middle elements with min of Subarray corners

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array A[] of length N. Then your task is to output the maximum sum that can be achieved by using the given operation at any number of times (possibly zero):

  • Select a subarray, whose length is at least 3.
  • Replace all the middle elements (Except the first and last) with a minimum of elements at both ends.

Examples:

Input: N = 3, A[] = {8, 1, 7}
Output: 22
Explanation: Let us take the subarray A[1, 3] = {8, 1, 7}. Middle element(s) are: {1}. Now minimum of A[1] and A[3] is = min(A[1], A[3]) = min(8, 7) = 7. Then, we replaced all the middle elements equal to min according to the operation. So, updated A[] = {8, 7, 7}. The sum of updated A[] = 22. Which is the maximum possible using a given operation. Therefore, output is 22.

Input: N = 2, A[] = {5, 2}
Output: 7
Explanation: No subarray of length at least 3 can be chosen, Thus can’t apply any operation. So the maximum possible sum is 7.

Approach: Implement the idea below to solve the problem

The problem is based on the observations and can be solved using the Prefix and Suffix arrays. It must be noticed that for any index i, we can not make the element A[i] to be anything more than the minimum of the greatest element to its left and the greatest element to its right.

Steps were taken to solve the problem:

  • Create two arrays let say Prefix[] and Suffix[]
  • Set prefix[0] = A[0]
  • Run a loop for i = 1 to i < N and follow the below-mentioned steps under the scope of the loop
    • prefix[i] = max(prefix[i – 1], A[i])
  • Set suffix[N – 1] = A[N – 1]
  • Run a loop for i = N – 2 to I >=0 and follow the below-mentioned steps under the scope of the loop
    • suffix[i] = max(suffix[i + 1], A[i])
  • Create a variable let say Sum to store the max possible sum.
  • Run a loop for i = 1 to i < N – 1 and follow below mentioned steps under the scope of loop
    • Max1 = Prefix[i]
    • Max2 = Suffix[i]
    • sum += min(Max1, Max2)
  • Sum += A[0] + A[N – 1]
  • Output the value store in Sum.

Code to implement the approach:

C++




//code by FLutterfly
#include <iostream>
#include <algorithm>
 
using namespace std;
 
// Function prototype
void Max_sum(int N, int A[]);
 
int main()
{
    // Input
    int N = 2;
    int A[] = {5, 2};
 
    // Function call
    Max_sum(N, A);
 
    return 0;
}
 
void Max_sum(int N, int A[])
{
    // Prefix and Suffix Arrays
    int prefix[N];
    int suffix[N];
 
    // Initializing prefix array
    prefix[0] = A[0];
    for (int i = 1; i < N; i++)
    {
        prefix[i] = max(prefix[i - 1], A[i]);
    }
 
    // Initializing suffix array
    suffix[N - 1] = A[N - 1];
    for (int i = N - 2; i >= 0; i--)
    {
        suffix[i] = max(suffix[i + 1], A[i]);
    }
 
    // Variable to store max sum
    long long sum = 0;
 
    // Calculating sum for all middle elements
    for (int i = 1; i < N - 1; i++)
    {
        int maxi1 = prefix[i], maxi2 = suffix[i];
        sum += min(maxi1, maxi2);
    }
 
    // Adding last elements of both ends into sum
    sum += A[0] + A[N - 1];
 
    // Printing the value of sum
    cout << sum << endl;
}


Java




// Java code to implement the approach
import java.util.*;
 
class Main {
    public static void main(String[] args)
    {
        // Input
        int N = 2;
        int A[] = { 5, 2 };
 
        // Function call
        Max_sum(N, A);
    }
    public static void Max_sum(int N, int[] A)
    {
        // Prefix and Suffix Arrays
        int[] prefix = new int[N];
        int[] suffix = new int[N];
 
        // Initializing prefix array
        prefix[0] = A[0];
        for (int i = 1; i < N; i++) {
            prefix[i] = Math.max(prefix[i - 1], A[i]);
        }
 
        // Initializing suffix array
        suffix[N - 1] = A[N - 1];
        for (int i = N - 2; i >= 0; i--) {
            suffix[i] = Math.max(suffix[i + 1], A[i]);
        }
 
        // Variable to store max sum
        long sum = 0;
 
        // Calculating sum for all middle elements
        for (int i = 1; i < N - 1; i++) {
            int maxi1 = prefix[i], maxi2 = suffix[i];
            sum += Math.min(maxi1, maxi2);
        }
 
        // Adding last elements of both ends into sum
        sum += A[0] + A[N - 1];
 
        // Printing the value of sum
        System.out.println(sum);
    }
}


Python




# code by flutterfly
def Max_sum(N, A):
    # Prefix and Suffix Arrays
    prefix = [0] * N
    suffix = [0] * N
 
    # Initializing prefix array
    prefix[0] = A[0]
    for i in range(1, N):
        prefix[i] = max(prefix[i - 1], A[i])
 
    # Initializing suffix array
    suffix[N - 1] = A[N - 1]
    for i in range(N - 2, -1, -1):
        suffix[i] = max(suffix[i + 1], A[i])
 
    # Variable to store max sum
    sum_val = 0
 
    # Calculating sum for all middle elements
    for i in range(1, N - 1):
        maxi1, maxi2 = prefix[i], suffix[i]
        sum_val += min(maxi1, maxi2)
 
    # Adding last elements of both ends into sum
    sum_val += A[0] + A[N - 1]
 
    # Printing the value of sum
    print(sum_val)
 
# Input
N = 2
A = [5, 2]
 
# Function call
Max_sum(N, A)


C#




//code by flutterfly
using System;
 
class Program
{
    static void Main()
    {
        // Input
        int N = 2;
        int[] A = { 5, 2 };
 
        // Function call
        Max_sum(N, A);
    }
 
    static void Max_sum(int N, int[] A)
    {
        // Prefix and Suffix Arrays
        int[] prefix = new int[N];
        int[] suffix = new int[N];
 
        // Initializing prefix array
        prefix[0] = A[0];
        for (int i = 1; i < N; i++)
        {
            prefix[i] = Math.Max(prefix[i - 1], A[i]);
        }
 
        // Initializing suffix array
        suffix[N - 1] = A[N - 1];
        for (int i = N - 2; i >= 0; i--)
        {
            suffix[i] = Math.Max(suffix[i + 1], A[i]);
        }
 
        // Variable to store max sum
        long sum = 0;
 
        // Calculating sum for all middle elements
        for (int i = 1; i < N - 1; i++)
        {
            int maxi1 = prefix[i], maxi2 = suffix[i];
            sum += Math.Min(maxi1, maxi2);
        }
 
        // Adding last elements of both ends into sum
        sum += A[0] + A[N - 1];
 
        // Printing the value of sum
        Console.WriteLine(sum);
    }
}


Javascript




function Max_sum(N, A) {
    // Prefix and Suffix Arrays
    let prefix = new Array(N);
    let suffix = new Array(N);
 
    // Initializing prefix array
    prefix[0] = A[0];
    for (let i = 1; i < N; i++) {
        prefix[i] = Math.max(prefix[i - 1], A[i]);
    }
 
    // Initializing suffix array
    suffix[N - 1] = A[N - 1];
    for (let i = N - 2; i >= 0; i--) {
        suffix[i] = Math.max(suffix[i + 1], A[i]);
    }
 
    // Variable to store max sum
    let sum = 0;
 
    // Calculating sum for all middle elements
    for (let i = 1; i < N - 1; i++) {
        let maxi1 = prefix[i], maxi2 = suffix[i];
        sum += Math.min(maxi1, maxi2);
    }
 
    // Adding last elements of both ends into sum
    sum += A[0] + A[N - 1];
 
    // Printing the value of sum
    console.log(sum);
}
 
// Input
let N = 2;
let A = [5, 2];
 
// Function call
Max_sum(N, A);


Output

7





Time Complexity: O(N)
Auxiliary Space: O(2*N), As Prefix and Suffix arrays of length N are used.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads