
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum sum of smallest and second smallest in an array in C++ Program
In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum of smallest and second smallest in an array.
Problem Description − We need to find the sum of the smallest and second smallest elements of a sum subarray of the array. And return the maximum of all such subarray sums.
Example
Let’s take an example to understand the problem,
Input
arr[] = {3, 5, 4, 2, 9, 1, 6}
Output
11
Explanation
Their out of all subarrays possible, {2, 9} has a maximum sum of smallest elements. Sum = 2 + 9 = 11
Solution Approach
A simple solution to the problem is by generating all subarrays. Find the smallest and second smallest element, find sum. Return the maximum sum of all.
Another more efficient solution is based on the observation of examples. We need to find the maximum sum which will be the sum of two consecutive element pairs of the array. And we will find the maximum sum pair.
Algorithm
Initialize −
maxSum = −1
Step 1 −
loop i −> 0 to n−1
Step 1.1 −
if maxSum < (arr[i] + arr[i+1]). Then, maxSum = (arr[i] + arr[i+1])
Step 2 −
Return maxSum.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcMaxSumPairs(int arr[], int n) { int maxSum = −1; for (int i=0; i< (n − 1); i++) if(maxSum < (arr[i] + arr[i + 1])) maxSum = (arr[i] + arr[i + 1]); return maxSum; } int main() { int arr[] = {3, 4, 2, 9, 5, 6}; int n = sizeof(arr) / sizeof(int); cout<<"The maximum sum of smallest and second smallest in an array is "<<calcMaxSumPairs(arr, n); return 0; }
Output
The maximum sum of smallest and second smallest in an array is 14
- Related Articles
- Maximum sum of smallest and second smallest in an array in C++
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- Find the smallest and second smallest elements in an array in C++
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- C program to find the second largest and smallest numbers in an array
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- Java program to find the smallest number in an array
- Checking digit sum of smallest number in the array in JavaScript
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Java program to find the 2nd smallest number in an array
- C++ program to find Second Smallest Element in a Linked List
- Program to find maximum possible value of smallest group in Python
- Find frequency of smallest value in an array in C++
- Program to Find the smallest number in an array of data in 8085 Microprocessor
- Get the smallest array from an array of arrays in JavaScript
