Maximum sum of smallest and second smallest in an array
Last Updated :
19 Sep, 2023
Given an array, find maximum sum of smallest and second smallest elements chosen from all possible subarrays. More formally, if we write all (nC2) subarrays of array of size >=2 and find the sum of smallest and second smallest, then our answer will be maximum sum among them.
Examples:
Input : arr[] = [4, 3, 1, 5, 6]
Output : 11
Subarrays with smallest and second smallest are,
[4, 3] smallest = 3 second smallest = 4
[4, 3, 1] smallest = 1 second smallest = 3
[4, 3, 1, 5] smallest = 1 second smallest = 3
[4, 3, 1, 5, 6] smallest = 1 second smallest = 3
[3, 1] smallest = 1 second smallest = 3
[3, 1, 5] smallest = 1 second smallest = 3
[3, 1, 5, 6] smallest = 1 second smallest = 3
[1, 5] smallest = 1 second smallest = 5
[1, 5, 6] smallest = 1 second smallest = 5
[5, 6] smallest = 5 second smallest = 6
Maximum sum among all above choices is, 5 + 6 = 11
Input : arr[] = {5, 4, 3, 1, 6}
Output : 9
Brute Force Approach:
The brute force approach to solve this problem is to generate all subarrays of size >= 2 and calculate the sum of the smallest and second smallest elements for each subarray. Finally, we return the maximum sum obtained among all subarrays.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <climits>
using namespace std;
int pairWithMaxSum( int arr[], int N)
{
int maxSum = INT_MIN;
for ( int i = 0; i < N - 1; i++)
{
for ( int j = i + 1; j < N; j++)
{
int sum = arr[i] + arr[j];
if (sum > maxSum)
{
maxSum = sum;
}
}
}
return maxSum;
}
int main()
{
int arr[] = {4, 3, 1, 5, 6};
int N = sizeof (arr) / sizeof ( int );
cout << pairWithMaxSum(arr, N) << endl;
return 0;
}
|
Java
import java.io.*;
public class GFG
{
static int PairWithMaxSum( int [] arr, int N)
{
int maxSum = Integer.MIN_VALUE;
for ( int i = 0 ; i < N - 1 ; i++)
{
for ( int j = i + 1 ; j < N; j++)
{
int sum = arr[i] + arr[j];
if (sum > maxSum)
{
maxSum = sum;
}
}
}
return maxSum;
}
public static void main (String[] args)
{
int [] arr = { 4 , 3 , 1 , 5 , 6 };
int N = arr.length;
System.out.println(PairWithMaxSum(arr, N));
}
}
|
Python3
import sys
def pairWithMaxSum(arr, N):
maxSum = - sys.maxsize - 1
for i in range (N - 1 ):
for j in range (i + 1 , N):
sum = arr[i] + arr[j]
if sum > maxSum:
maxSum = sum
return maxSum
if __name__ = = "__main__" :
arr = [ 4 , 3 , 1 , 5 , 6 ]
N = len (arr)
print (pairWithMaxSum(arr, N))
|
C#
using System;
public class Program
{
static int PairWithMaxSum( int [] arr, int N)
{
int maxSum = int .MinValue;
for ( int i = 0; i < N - 1; i++)
{
for ( int j = i + 1; j < N; j++)
{
int sum = arr[i] + arr[j];
if (sum > maxSum)
{
maxSum = sum;
}
}
}
return maxSum;
}
public static void Main()
{
int [] arr = { 4, 3, 1, 5, 6 };
int N = arr.Length;
Console.WriteLine(PairWithMaxSum(arr, N));
}
}
|
Javascript
function pairWithMaxSum(arr, N) {
let maxSum = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < N - 1; i++) {
for (let j = i + 1; j < N; j++) {
let sum = arr[i] + arr[j];
if (sum > maxSum) {
maxSum = sum;
}
}
}
return maxSum;
}
let arr = [4, 3, 1, 5, 6];
let N = arr.length;
console.log(pairWithMaxSum(arr, N));
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
An efficient solution is based on the observation that this problem reduces to finding a maximum sum of two consecutive elements in array.
If (x,y) is the pair ,such that (x+y) is the answer , then x and y must be consecutive elements in the array.
Proof:
For a subarray with 2 elements , 1st and 2nd smallest elements are those 2 elements.
Now x and y are present in some subarray such thatthey are the endpoints.
Now, x, y must be the smallest 2 elements of that subarray. If there are other elements Z1 , Z2, ……., ZK between x and y, they are greater than or equal to x and y,
Case1 :
- If there is one element z between x and y , then the smaller subarray with the elements max(x,y) and z , should be the answer , because max(x,y) + z >= x + y
Case2:
- If there are more than one elements between x and y , then the subarray within x and y will have all consecutive elements (Zi + Zi+1) >= (x+y), so (x,y) pair can’t be the answer.
- So, by contradictions, x and y must be consecutive elements in the array.
Implementation:
CPP
#include <bits/stdc++.h>
using namespace std;
int pairWithMaxSum( int arr[], int N)
{
if (N < 2)
return -1;
int res = arr[0] + arr[1];
for ( int i=1; i<N-1; i++)
res = max(res, arr[i] + arr[i+1]);
return res;
}
int main()
{
int arr[] = {4, 3, 1, 5, 6};
int N = sizeof (arr) / sizeof ( int );
cout << pairWithMaxSum(arr, N) << endl;
return 0;
}
|
JAVA
import java.lang.*;
class num{
static int pairWithMaxSum( int [] arr, int N)
{
if (N < 2 )
return - 1 ;
int res = arr[ 0 ] + arr[ 1 ];
for ( int i= 1 ; i<N- 1 ; i++)
res = Math.max(res, arr[i] + arr[i+ 1 ]);
return res;
}
public static void main(String[] args)
{
int arr[] = { 4 , 3 , 1 , 5 , 6 };
int N = arr.length;
System.out.println(pairWithMaxSum(arr, N));
}
}
|
Python3
def pairWithMaxSum(arr, N):
if (N < 2 ):
return - 1
res = arr[ 0 ] + arr[ 1 ]
for i in range ( 1 , N - 1 ):
res = max (res, arr[i] + arr[i + 1 ])
return res
arr = [ 4 , 3 , 1 , 5 , 6 ]
N = len (arr)
print (pairWithMaxSum(arr, N))
|
C#
using System;
class GFG {
static int pairWithMaxSum( int []arr, int N)
{
if (N < 2)
return -1;
int res = arr[0] + arr[1];
for ( int i = 1; i < N - 1; i++)
res = Math.Max(res, arr[i] + arr[i + 1]);
return res;
}
public static void Main()
{
int []arr = {4, 3, 1, 5, 6};
int N = arr.Length;
Console.Write(pairWithMaxSum(arr, N));
}
}
|
Javascript
function pairWithMaxSum(arr, N)
{
if (N < 2)
return -1;
var res = arr[0] + arr[1];
for ( var i = 1; i < N - 1; i++)
res = Math.max(res, arr[i] + arr[i + 1]);
return res;
}
var arr = [4, 3, 1, 5, 6]
var N = arr.length;
document.write(pairWithMaxSum(arr, N));
|
PHP
<?php
function pairWithMaxSum( $arr , $N )
{
if ( $N < 2)
return -1;
$res = $arr [0] + $arr [1];
for ( $i = 1; $i < $N - 1; $i ++)
$res = max( $res , $arr [ $i ] +
$arr [ $i + 1]);
return $res ;
}
$arr = array (4, 3, 1, 5, 6);
$N = count ( $arr );
echo pairWithMaxSum( $arr , $N );
?>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Thanks to Md Mishfaq Ahmed for suggesting this approach.
Share your thoughts in the comments
Please Login to comment...