
- 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
Find the largest pair sum in an unsorted array in C++
In this problem, we are given an arr[] consisting of N unsorted elements. Our task is to find the largest pair sum in an unsorted array.
We will find a pair whose sum is the maximum.
Let's take an example to understand the problem,
Input : arr[] = {7, 3, 9, 12, 1} Output : 21
Explanation −
Pair with largest sum, (9, 12). Sum = 21
Solution Approach
A simple solution to the problem is by making a pair of maximum and second maximum elements of the array.
For this we will initialise the max and secondMax elements of the array with the first and second element of the array, greater one is max and other one is secondMax.
Now, loop through the array from index 2 to (n-1). And compare them with max and secondMax values.
If arr[i] is greater than max, secondMax = max and max = arr[i].
If arr[i] is greater than secondMax, secondMax = arr[i].
And at the end of the loop, return the (max + secondMax).
Example
Program to illustrate the working of our solution
#include<iostream> using namespace std; int findPairLargestSum(int arr[], int n){ int max, secondMax; if (arr[0] > arr[1]){ max = arr[0]; secondMax = arr[1]; } else{ max = arr[1]; secondMax = arr[0]; } for (int i = 2; i<n; i ++){ if (arr[i] > max){ secondMax = max; max = arr[i]; } else if (arr[i] > secondMax && arr[i] != max) secondMax = arr[i]; } return (max + secondMax); } int main(){ int arr[] = {12, 34, 10, 6, 40}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"The sum of elements of pair with max sum is "<<findPairLargestSum(arr, n); return 0; }
Output
The sum of elements of pair with max sum is 74
- Related Articles
- Finding the largest and smallest number in an unsorted array of integers in JavaScript
- K’th Smallest/Largest Element in Unsorted Array in C++
- Print all pairs in an unsorted array with equal sum in C++
- kth smallest/largest in a small range unsorted array in C++
- Find k closest numbers in an unsorted array in C++
- Find floor and ceil in an unsorted array using C++.
- Find Mean and Median of an unsorted Array in Java
- Find the Smallest Positive Number Missing From an Unsorted Array
- Find start and ending index of an element in an unsorted array in C++
- Find the largest three elements in an array in C++
- Swift Program to Find Mean of an Unsorted Array
- Swift Program to Find Median of an Unsorted Array
- Split Array Largest Sum in C++
- Java program to find the largest number in an array
- Python Program to find the largest element in an array
