Minimum value of “max + min” in a subarray
Last Updated :
18 Aug, 2022
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++
#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;
}
int main()
{
int arr[] = {1, 12, 2, 2};
int n = sizeof (arr) / sizeof (arr[0]);
cout << maxSum(arr, n);
return 0;
}
|
Java
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;
}
public static void main (String[] args)
{
int arr[] = { 1 , 12 , 2 , 2 };
int n = arr.length;
System.out.println( maxSum(arr, n));
}
}
|
Python 3
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
if __name__ = = '__main__' :
arr = [ 1 , 12 , 2 , 2 ]
n = len (arr)
print (maxSum(arr, n))
|
C#
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;
}
public static void Main ()
{
int []arr = {1, 12, 2, 2};
int n = arr.Length;
Console.WriteLine( maxSum(arr, n));
}
}
|
PHP
<?php
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 ;
}
$arr = array (1, 12, 2, 2);
$n = count ( $arr );
echo maxSum( $arr , $n );
?>
|
Javascript
<script>
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;
}
let arr = [1, 12, 2, 2];
let n = arr.length;
document.write( maxSum(arr, n));
</script>
|
Complexity Analysis:
- Time Complexity : O(n)
- Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...