
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum sum of smallest and second smallest in an array in C++
In this tutorial, we will be discussing a program to find maximum sum of smallest and second smallest in an array.
For this we will be provided with an array containing integers. Our task is to find the maximum sum of smallest and second smallest elements in every possible iteration of array.
Example
#include <bits/stdc++.h> using namespace std; //returning maximum sum of smallest and //second smallest elements 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; }
Output
11
- Related Questions & Answers
- Maximum sum of smallest and second smallest in an array in C++ Program
- Find the smallest and second smallest elements in an array in C++
- Java program to find Largest, Smallest, Second Largest, Second Smallest in an array
- C# program to find Largest, Smallest, Second Largest, Second Smallest in a List
- C program to find the second largest and smallest numbers in an array
- Python program to find Largest, Smallest, Second Largest, and Second Smallest in a List?
- Find frequency of smallest value in an array in C++
- Rearrange An Array In Order – Smallest, Largest, 2nd Smallest, 2nd Largest,. Using C++
- Finding second smallest word in a string - JavaScript
- Checking digit sum of smallest number in the array in JavaScript
- Greatest sum and smallest index difference in JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Path with smallest sum in JavaScript
- Get the smallest array from an array of arrays in JavaScript
- Third smallest number in an array using JavaScript
Advertisements