Maximum sum of smallest and second smallest in an array
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
A simple solution is to generate all subarrays, find sum of smallest and second smallest of every subarray. Finally return maximum of all sums.
An efficient solution is based on the observation that this problem reduces to finding a maximum sum of two consecutive elements in array.
CPP
// C++ program to get max sum with smallest // and second smallest element from any subarray #include <bits/stdc++.h> using namespace std; /* Method returns maximum obtainable sum value of smallest and the second smallest value taken over all possible subarrays */int pairWithMaxSum(int arr[], int N) { if (N < 2) return -1; // 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; } // Driver code to test above methods int main() { int arr[] = {4, 3, 1, 5, 6}; int N = sizeof(arr) / sizeof(int); cout << pairWithMaxSum(arr, N) << endl; return 0; } |
JAVA
// Java program to get max sum with smallest // and second smallest element from any subarray import java.lang.*; class num{ // Method returns maximum obtainable sum value // of smallest and the second smallest value // taken over all possible subarrays */ static int pairWithMaxSum(int[] arr, int N) { if (N < 2) return -1; // 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; } // Driver program public static void main(String[] args) { int arr[] = {4, 3, 1, 5, 6}; int N = arr.length; System.out.println(pairWithMaxSum(arr, N)); } } //This code is contributed by //Smitha Dinesh Semwal |
Python3
# Python 3 program to get max # sum with smallest and second # smallest element from any # subarray # Method returns maximum obtainable # sum value of smallest and the # second smallest value taken # over all possible subarrays def pairWithMaxSum(arr, N): if (N < 2): return -1 # 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 # Driver code arr = [4, 3, 1, 5, 6] N = len(arr) print(pairWithMaxSum(arr, N)) # This code is contributed by Smitha Dinesh Semwal |
C#
// C# program to get max sum with smallest // and second smallest element from any subarray using System; class GFG { // Method returns maximum obtainable sum value // of smallest and the second smallest value // taken over all possible subarrays static int pairWithMaxSum(int []arr, int N) { if (N < 2) return -1; // 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; } // Driver code public static void Main() { int []arr = {4, 3, 1, 5, 6}; int N = arr.Length; Console.Write(pairWithMaxSum(arr, N)); } } // This code is contributed by Nitin Mittal. |
PHP
<?php // PHP program to get max sum with smallest // and second smallest element from any subarray /* Method returns maximum obtainable sum value of smallest and the second smallest value taken over all possible subarrays */function pairWithMaxSum( $arr, $N) { if ($N < 2) return -1; // Find two consecutive // elements with maximum // sum. $res = $arr[0] + $arr[1]; for($i = 1; $i < $N - 1; $i++) $res = max($res, $arr[$i] + $arr[$i + 1]); return $res; } // Driver Code $arr = array(4, 3, 1, 5, 6); $N = count($arr); echo pairWithMaxSum($arr, $N); // This code is contributed by anuj_67. ?> |
Output:
11
Time Complexity : O(n)
Thanks to Md Mishfaq Ahmed for suggesting this approach.
This article is contributed by Utkarsh Trivedi. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find the smallest and second smallest elements in an array
- Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, ..
- Size of the smallest subset with maximum Bitwise OR
- Smallest subarray containing minimum and maximum values
- Queries to return the absolute difference between L-th smallest number and the R-th smallest number
- Length of the smallest sub-string consisting of maximum distinct characters
- Kth smallest element in the array using constant space when array can't be modified
- Smallest Pair Sum in an array
- Find frequency of smallest value in an array
- Lexicographical smallest alternate Array
- Smallest perfect cube in an array
- Forming smallest array with given constraints
- Smallest greater elements in whole array
- k-th smallest absolute difference of two elements in an array
- Smallest element greater than X not present in the array



