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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


Python 3

# 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 [tabby title="C#"]

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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.
?>

chevron_right


Output:

4

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



My Personal Notes arrow_drop_up

A computer Science ug student from lnmiit Interested in learning and sharing algorithmic knowledge Found near online Judges and food

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m, SURENDRA_GANGWAR



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.