Open In App

Minimum value of “max + min” in a subarray

Given a array of n positive elements we need to find the lowest possible sum of max and min elements in a subarray given that size of subarray should be greater than equal to 2.

Examples:  

Input : arr[] = {1 12 2 2}
Output : 4
Sum of 2+2 of subarray [2, 2]

Input  : arr[] = {10 20 30 40 23 45}
Output : 30 
Sum of 10+20 of subarray[10, 20]

A simple solution is to generate all subarrays, compute sum of maximum and minimum and finally return lowest sum.

An efficient solution is based on the fact that adding any element to a subarray would not increase sum of maximum and minimum. Consider the array [a1, a2, a3, a4, a5….an] Each ai will be minimum of some subarray [al, ar] such that i lies between [l, r] and all elements in the subarray are greater than or equal to ai. The cost of such subarray would be ai + max(subarray). Since the max of an array will never decrease on adding elements to the array, It will only increase if we add larger elements so It is always optimal to consider only those subarrays having length 2.

In short consider all subarrays of length 2 and compare sum and take the minimum one which will reduce the time complexity by O(N) now we have to run the loop only once. 

Implementation:




// CPP program to find sum of maximum and
// minimum in any subarray of an array of
// positive numbers.
#include <bits/stdc++.h>
using namespace std;
 
int maxSum(int arr[], int n)
{
    if (n < 2)
        return -1;
    int ans = arr[0] + arr[1];
    for (int i = 1; i + 1 < n; i++)
        ans = min(ans, (arr[i] + arr[i + 1]));
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = {1, 12, 2, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxSum(arr, n);
    return 0;
}




// java program to find sum of maximum and
// minimum in any subarray of an array of
// positive numbers.
import java.io.*;
 
class GFG {
     
    static int maxSum(int arr[], int n)
    {
        if (n < 2)
            return -1;
        int ans = arr[0] + arr[1];
        for (int i = 1; i + 1 < n; i++)
            ans = Math.min(ans, (arr[i]
                           + arr[i + 1]));
        return ans;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = {1, 12, 2, 2};
        int n = arr.length;
         
        System.out.println( maxSum(arr, n));
    }
}
 
// This code is contributed by anuj_67.




# Python 3 program to find sum of maximum
# and minimum in any subarray of an array
# of positive numbers.
 
def maxSum(arr, n):
    if (n < 2):
        return -1
    ans = arr[0] + arr[1]
    for i in range(1, n - 1, 1):
        ans = min(ans, (arr[i] + arr[i + 1]))
    return ans
 
# Driver code
if __name__ == '__main__':
    arr = [1, 12, 2, 2]
    n = len(arr)
    print(maxSum(arr, n))
 
# This code is contributed by
# Surendra_Gangwar




// C# program to find sum of maximum and
// minimum in any subarray of an array of
// positive numbers.
using System ;
 
class GFG {
     
    static int maxSum(int []arr, int n)
    {
        if (n < 2)
            return -1;
        int ans = arr[0] + arr[1];
        for (int i = 1; i + 1 < n; i++)
            ans = Math.Min(ans, (arr[i]
                        + arr[i + 1]));
        return ans;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr = {1, 12, 2, 2};
        int n = arr.Length;
         
        Console.WriteLine( maxSum(arr, n));
    }
}
 
// This code is contributed by anuj_67.




<?php
// PHP program to find sum of
// maximum and minimum in any
// subarray of an array of
// positive numbers.
 
function maxSum( $arr, $n)
{
    if ($n < 2)
        return -1;
    $ans = $arr[0] + $arr[1];
    for ( $i = 1; $i + 1 < $n; $i++)
        $ans = min($ans, ($arr[$i] +
                     $arr[$i + 1]));
    return $ans;
}
 
    // Driver code
    $arr = array(1, 12, 2, 2);
    $n = count($arr);
    echo maxSum($arr, $n);
 
// This code is contributed by anuj_67.
?>




<script>
// java program to find sum of maximum and
// minimum in any subarray of an array of
// positive numbers.
function maxSum(arr,n)
    {
        if (n < 2)
            return -1;
        let ans = arr[0] + arr[1];
        for (let i = 1; i + 1 < n; i++)
            ans = Math.min(ans, (arr[i]
                        + arr[i + 1]));
        return ans;
    }
     
    // Driver code
     
        let arr = [1, 12, 2, 2];
        let n = arr.length;
         
        document.write( maxSum(arr, n));
// This code is contributed by sravan
</script>

Output
4

Complexity Analysis:


Article Tags :