Sunday, October 19, 2025
HomeData Modelling & AIMinimum value of “max + min” in a subarray

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:

C++




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




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




# 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#




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


Javascript




<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:

  • Time Complexity : O(n) 
  • Auxiliary Space : O(1) 
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS