Maximum sum of smallest and second smallest in an array
Last Updated :
01 Jul, 2025
Given an array arr[] of positive integers (with length at least 2), find the maximum sum of the smallest and second smallest elements across all subarrays of size ≥ 2.
Examples:
Input: arr[] = [4, 9, 7]
Output: 16
Explanation: All subarrays of size ≥ 2:
[4, 9] → min = 4, second min = 9 → sum = 13
[4, 9, 7] → min = 4, second min = 7 → sum = 11
[9, 7] → min = 7, second min = 9 → sum = 16
Maximum sum is 16, from subarray [9, 7].
Input: arr[] = [5, 4, 3, 1, 6]
Output: 9
Explanation: The subarray [5, 4] gives min = 4, second min = 5 → sum = 9. All other subarrays yield a smaller sum. Hence, the answer is 9.
[Naive Approach] By Exploring all Subarrays - O(n2) Time and O(1) Space
This approach checks all subarrays of size ≥ 2 and tracks the smallest and second smallest elements in each. It updates these two values as it expands the subarray and calculates their sum. The maximum of all such sums is the final answer.
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxSum(vector<int>& arr) {
int n = arr.size();
int maxSum = 0;
// iterate over each element in
// the array except the last one
for (int i = 0; i < n - 1; i++) {
int mini = arr[i];
int secondMini = arr[i + 1];
// iterate over the remaining
// elements in the array
for (int j = i + 1; j < n; j++) {
if (mini >= arr[j]) {
secondMini = mini;
mini = arr[j];
}
else if (secondMini > arr[j]) {
secondMini = arr[j];
}
// calculate the current sum of the minimum
// and second minimum elements
int currSum = mini + secondMini;
// update the maximum sum if
// the current sum is greater
maxSum = max(maxSum, currSum);
}
}
return maxSum;
}
int main() {
vector<int> arr = { 5, 4, 3, 1, 6 };
int result = maxSum(arr);
cout << result << endl;
}
Java
import java.util.Arrays;
public class GfG {
public static int maxSum(int[] arr) {
int n = arr.length;
int maxSum = 0;
// iterate over each element in
// the array except the last one
for (int i = 0; i < n - 1; i++) {
int mini = arr[i];
int secondMini = arr[i + 1];
// iterate over the remaining
// elements in the array
for (int j = i + 1; j < n; j++) {
if (mini >= arr[j]) {
secondMini = mini;
mini = arr[j];
} else if (secondMini > arr[j]) {
secondMini = arr[j];
}
// calculate the current sum of the minimum
// and second minimum elements
int currSum = mini + secondMini;
// update the maximum sum if the
// current sum is greater
maxSum = Math.max(maxSum, currSum);
}
}
return maxSum;
}
public static void main(String[] args) {
int[] arr = { 5, 4, 3, 1, 6 };
int result = maxSum(arr);
System.out.println(result);
}
}
Python
def maxSum(arr):
n = len(arr)
maxSum = 0
# iterate over each element in
# the array except the last one
for i in range(n - 1):
mini = arr[i]
secondMini = arr[i + 1]
# iterate over the remaining
# elements in the array
for j in range(i + 1, n):
if mini >= arr[j]:
secondMini = mini
mini = arr[j]
elif secondMini > arr[j]:
secondMini = arr[j]
# calculate the current sum of the minimum
# and second minimum elements
currSum = mini + secondMini
# update the maximum sum if
# the current sum is greater
maxSum = max(maxSum, currSum)
return maxSum
if __name__ == "__main__":
arr = [5, 4, 3, 1, 6]
result = maxSum(arr)
print(result)
C#
using System;
using System.Linq;
class GfG {
static int maxSum(int[] arr) {
int n = arr.Length;
int maxSum = 0;
// iterate over each element in
// the array except the last one
for (int i = 0; i < n - 1; i++) {
int mini = arr[i];
int secondMini = arr[i + 1];
// iterate over the remaining
// elements in the array
for (int j = i + 1; j < n; j++) {
if (mini >= arr[j]) {
secondMini = mini;
mini = arr[j];
} else if (secondMini > arr[j]) {
secondMini = arr[j];
}
// calculate the current sum of the minimum
// and second minimum elements
int currSum = mini + secondMini;
// update the maximum sum if
// the current sum is greater
maxSum = Math.Max(maxSum, currSum);
}
}
return maxSum;
}
static void Main() {
int[] arr = { 5, 4, 3, 1, 6 };
int result = maxSum(arr);
Console.WriteLine(result);
}
}
JavaScript
function maxSum(arr) {
let n = arr.length;
let maxSum = 0;
// iterate over each element in
// the array except the last one
for (let i = 0; i < n - 1; i++) {
let mini = arr[i];
let secondMini = arr[i + 1];
// iterate over the remaining
// elements in the array
for (let j = i + 1; j < n; j++) {
if (mini >= arr[j]) {
secondMini = mini;
mini = arr[j];
} else if (secondMini > arr[j]) {
secondMini = arr[j];
}
// calculate the current sum of the minimum
// and second minimum elements
let currSum = mini + secondMini;
// update the maximum sum if
// the current sum is greater
maxSum = Math.max(maxSum, currSum);
}
}
return maxSum;
}
// Driver Code
let arr = [5, 4, 3, 1, 6];
let result = maxSum(arr);
console.log(result);
[Expected Approach] By maximizing consecutive element sum - O(n) Time and O(1) Space
An efficient solution relies on the key observation that the problem reduces to finding the maximum sum of two consecutive elements in the array.
If (x, y) is the pair that yields the maximum sum of the smallest and second smallest elements across all subarrays, then x and y must be adjacent in the array.
Contradictory Proof:
For any subarray of size 2, the smallest and second smallest elements are simply the two elements themselves.
Now, suppose x and y are two elements in the array that are the endpoints of a subarray and are also the two smallest elements within that subarray.
Let’s consider the elements z1 , z2 , ..., zk that lie between x and y.
Case1 : One element between x and y
If there's only one element z, and it's not smaller than both x and y, then the subarray formed by max(x, y) and z will have a sum that is at least x + y. So, in this case, (x, y) does not give the maximum sum of smallest and second smallest.
Case2: More than one element between x and y
In this case, the subarray between x and y includes multiple pairs like (zi , zi+1 ). Since all intermediate elements are greater than x and y, their sums will be greater than or equal to x + y.
Hence, (x, y) cannot produce the maximum sum.
=> By contradiction, the only possible candidate pairs for the maximum sum of smallest and second smallest in any subarray must be adjacent elements in the array. So it’s sufficient to only check all adjacent pairs.
C++
#include <iostream>
#include <vector>
using namespace std;
int maxSum(vector<int>& arr) {
int n = arr.size();
// find two consecutive elements with maximum sum
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() {
vector<int> arr = {5, 4, 3, 1, 6};
cout << maxSum(arr) << endl;
return 0;
}
Java
import java.util.Arrays;
class GfG {
public static int maxSum(int[] arr) {
int n = arr.length;
// find two consecutive elements with maximum sum
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 = {5, 4, 3, 1, 6};
System.out.println(maxSum(arr));
}
}
Python
def maxSum(arr):
n = len(arr)
# find two consecutive elements with maximum sum
res = arr[0] + arr[1]
for i in range(1, n - 1):
res = max(res, arr[i] + arr[i + 1])
return res
if __name__ == "__main__":
arr = [5, 4, 3, 1, 6]
print(maxSum(arr))
C#
using System;
using System.Linq;
class GfG {
static int maxSum(int[] arr) {
int n = arr.Length;
// find two consecutive elements with maximum sum
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;
}
static void Main() {
int[] arr = {5, 4, 3, 1, 6};
Console.WriteLine(maxSum(arr));
}
}
JavaScript
function maxSum(arr) {
const n = arr.length;
// find two consecutive elements with maximum sum
let res = arr[0] + arr[1];
for (let i = 1; i < n - 1; i++)
res = Math.max(res, arr[i] + arr[i + 1]);
return res;
}
// Driver Code
const arr = [5, 4, 3, 1, 6];
console.log(maxSum(arr));
Similar Reads
Maximum sum of minimums of pairs in an array Given an array arr[] of N integers where N is even, the task is to group the array elements in the pairs (X1, Y1), (X2, Y2), (X3, Y3), ... such that the sum min(X1, Y1) + min(X2, Y2) + min(X3, Y3) + ... is maximum.Examples: Input: arr[] = {1, 5, 3, 2} Output: 4 (1, 5) and (3, 2) -> 1 + 2 = 3 (1,
4 min read
How to get largest and smallest number in an Array? Given an array arr[] of length N, The task is to find the maximum and the minimum number in the array. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: Maximum is: 5Minimum is: 1Explanation: The maximum of the array is 5 and the minimum of the array is 1. Input: arr[] = {5, 3, 7, 4, 2}Output: Maximum
15 min read
Smallest subarray containing minimum and maximum values Given an array A of size N. The task is to find the length of smallest subarray which contains both maximum and minimum values. Examples: Input : A[] = {1, 5, 9, 7, 1, 9, 4} Output : 2 subarray {1, 9} has both maximum and minimum value. Input : A[] = {2, 2, 2, 2} Output : 1 2 is both maximum and min
6 min read
Maximum adjacent pair sum by array rearrangement Given an array arr[] of size N, find the maximum value of adjacent sum by rearranging array in any order. Examples: Input: arr[] = {1, 2, 3, 4}Output: 17Explanation: The arrangement is {1, 3, 4, 2} where sum of all the adjacent element is (1 + 3) + (3 + 4) + (4 + 2) = 17 which is maximum. Input: arr
5 min read
kth smallest/largest in a small range unsorted array Find kth smallest or largest element in an unsorted array, where k<=size of array. It is given that elements of array are in small range. Examples: Input : arr[] = {3, 2, 9, 5, 7, 11, 13} k = 5 Output: 9 Input : arr[] = {16, 8, 9, 21, 43} k = 3 Output: 16 Input : arr[] = {50, 50, 40} k = 2 Output
5 min read