Largest pair sum in an array
Last Updated :
27 Feb, 2025
Given an unsorted of distinct integers, find the largest pair sum in it. For example, the largest pair sum is 74. If there are less than 2 elements, then we need to return -1.
Input : arr[] = {12, 34, 10, 6, 40},
Output : 74
Input : arr[] = {10, 10, 10},
Output : 20
Input arr[] = {10},
Output : -1
[Naive Approach] - Nested Loops - O(n^2) Time and O(1) Space
The idea is to use two nested loops to iterate over all possible pairs of integers in the array, compute their sum and keep track of the maximum sum encountered so far.
C++
#include <bits/stdc++.h>
using namespace std;
/* Function to return largest pair sum. Assumes that
there are at-least two elements in arr[] */
int findLargestSumPair(vector<int> &arr)
{
int maxSum = INT_MIN;
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
if (sum > maxSum) {
maxSum = sum;
}
}
}
return (maxSum == INT_MIN)? -1 : maxSum;
}
/* Driver program to test above function */
int main()
{
vector<int> arr = { 12, 34, 10, 6, 40 };
cout << "Max Pair Sum is "
<< findLargestSumPair(arr);
return 0;
}
C
#include <stdio.h>
#include <limits.h>
/* Function to return largest pair sum. Assumes that
there are at-least two elements in arr[] */
int findLargestSumPair(int arr[], int n) {
int maxSum = INT_MIN;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
if (sum > maxSum) {
maxSum = sum;
}
}
}
return (maxSum == INT_MIN) ? -1 : maxSum;
}
/* Driver program to test above function */
int main() {
int arr[] = {12, 34, 10, 6, 40};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Max Pair Sum is %d\n", findLargestSumPair(arr, n));
return 0;
}
Java
import java.util.Arrays;
import java.util.List;
class Main {
/* Function to return largest pair sum. Assumes that
there are at-least two elements in arr[] */
static int findLargestSumPair(List<Integer> arr) {
int maxSum = Integer.MIN_VALUE;
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr.get(i) + arr.get(j);
if (sum > maxSum) {
maxSum = sum;
}
}
}
return (maxSum == Integer.MIN_VALUE) ? -1 : maxSum;
}
/* Driver program to test above function */
public static void main(String[] args) {
List<Integer> arr = Arrays.asList(12, 34, 10, 6, 40);
System.out.println("Max Pair Sum is " + findLargestSumPair(arr));
}
}
Python
# Function to return largest pair sum. Assumes that
# there are at-least two elements in arr[]
def findLargestSumPair(arr):
maxSum = float('-inf')
n = len(arr)
for i in range(n - 1):
for j in range(i + 1, n):
sum = arr[i] + arr[j]
if sum > maxSum:
maxSum = sum
return -1 if maxSum == float('-inf') else maxSum
# Driver program to test above function
arr = [12, 34, 10, 6, 40]
print("Max Pair Sum is ", findLargestSumPair(arr))
C#
using System;
using System.Collections.Generic;
class Program {
/* Function to return largest pair sum. Assumes that
there are at-least two elements in arr[] */
static int FindLargestSumPair(List<int> arr) {
int maxSum = int.MinValue;
int n = arr.Count;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int sum = arr[i] + arr[j];
if (sum > maxSum) {
maxSum = sum;
}
}
}
return (maxSum == int.MinValue) ? -1 : maxSum;
}
/* Driver program to test above function */
static void Main() {
List<int> arr = new List<int> { 12, 34, 10, 6, 40 };
Console.WriteLine("Max Pair Sum is " + FindLargestSumPair(arr));
}
}
JavaScript
// Function to return largest pair sum. Assumes that
// there are at-least two elements in arr[]
function findLargestSumPair(arr) {
let maxSum = Number.NEGATIVE_INFINITY;
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
let sum = arr[i] + arr[j];
if (sum > maxSum) {
maxSum = sum;
}
}
}
return (maxSum === Number.NEGATIVE_INFINITY) ? -1 : maxSum;
}
// Driver program to test above function
let arr = [12, 34, 10, 6, 40];
console.log("Max Pair Sum is ", findLargestSumPair(arr));
[Expected Solution] - Largest two Elements - O(n) Time and O(1) Space
This problem mainly boils down to finding the largest and second-largest element in an array. We can find the largest and second-largest in O(n) time by traversing the array once.
- Initialize : first = -INF, second = INF
- Loop through the elements
If the current element is greater than the first max element, then update second max to the first max and update the first max to the current element. - Return (first + second)
C++
#include <iostream>
#include <vector>
using namespace std;
/* Function to return largest pair sum. Assumes that
there are at-least two elements in arr[] */
int findLargestSumPair(vector<int>& arr)
{
int n = arr.size();
if (n < 2) return -1;
// Initialize first and second largest element
int first, second;
if (arr[0] > arr[1]) {
first = arr[0];
second = arr[1];
}
else {
first = arr[1];
second = arr[0];
}
// Traverse remaining array and find first and second
// largest elements in overall array
for (int i = 2; i < n; i++) {
/* If current element is greater than first then
update both first and second */
if (arr[i] > first) {
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second then
* update second */
else if (arr[i] > second)
second = arr[i];
}
return (first + second);
}
/* Driver program to test above function */
int main()
{
vector<int> arr = { 12, 34, 10, 6, 40 };
cout << "Max Pair Sum is "
<< findLargestSumPair(arr);
return 0;
}
C
/* Function to return largest pair sum. Assumes that
there are at-least two elements in arr[] */
#include <stdio.h>
#include <limits.h>
int findLargestSumPair(int arr[], int n) {
if (n < 2) return -1;
// Initialize first and second largest element
int first, second;
if (arr[0] > arr[1]) {
first = arr[0];
second = arr[1];
} else {
first = arr[1];
second = arr[0];
}
// Traverse remaining array and find first and second
// largest elements in overall array
for (int i = 2; i < n; i++) {
// If current element is greater than first then
// update both first and second
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second) {
// If arr[i] is in between first and second then
// update second
second = arr[i];
}
}
return (first + second);
}
// Driver program to test above function
int main() {
int arr[] = {12, 34, 10, 6, 40};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Max Pair Sum is %d\n", findLargestSumPair(arr, n));
return 0;
}
Java
// Function to return largest pair sum. Assumes that
// there are at-least two elements in arr[]
class LargestSumPair {
public static int findLargestSumPair(int[] arr) {
int n = arr.length;
if (n < 2) return -1;
// Initialize first and second largest element
int first, second;
if (arr[0] > arr[1]) {
first = arr[0];
second = arr[1];
} else {
first = arr[1];
second = arr[0];
}
// Traverse remaining array and find first and second
// largest elements in overall array
for (int i = 2; i < n; i++) {
// If current element is greater than first then
// update both first and second
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second) {
// If arr[i] is in between first and second then
// update second
second = arr[i];
}
}
return (first + second);
}
// Driver program to test above function
public static void main(String[] args) {
int[] arr = {12, 34, 10, 6, 40};
System.out.println("Max Pair Sum is " + findLargestSumPair(arr));
}
}
Python
# Function to return largest pair sum. Assumes that
# there are at-least two elements in arr[]
def find_largest_sum_pair(arr):
n = len(arr)
if n < 2:
return -1
# Initialize first and second largest element
first, second = (arr[0], arr[1]) if arr[0] > arr[1] else (arr[1], arr[0])
# Traverse remaining array and find first and second
# largest elements in overall array
for i in range(2, n):
# If current element is greater than first then
# update both first and second
if arr[i] > first:
second = first
first = arr[i]
elif arr[i] > second:
# If arr[i] is in between first and second then
# update second
second = arr[i]
return first + second
# Driver program to test above function
arr = [12, 34, 10, 6, 40]
print("Max Pair Sum is", find_largest_sum_pair(arr))
C#
// Function to return largest pair sum. Assumes that
// there are at least two elements in arr[]
using System;
using System.Collections.Generic;
class Program
{
static int FindLargestSumPair(List<int> arr)
{
int n = arr.Count;
if (n < 2) return -1;
// Initialize first and second largest element
int first, second;
if (arr[0] > arr[1])
{
first = arr[0];
second = arr[1];
}
else
{
first = arr[1];
second = arr[0];
}
// Traverse remaining array and find first and second
// largest elements in overall array
for (int i = 2; i < n; i++)
{
/* If current element is greater than first then
update both first and second */
if (arr[i] > first)
{
second = first;
first = arr[i];
}
/* If arr[i] is in between first and second then
update second */
else if (arr[i] > second)
second = arr[i];
}
return (first + second);
}
// Driver program to test above function
static void Main()
{
List<int> arr = new List<int> { 12, 34, 10, 6, 40 };
Console.WriteLine("Max Pair Sum is " + FindLargestSumPair(arr));
}
}
JavaScript
// Function to return largest pair sum. Assumes that
// there are at-least two elements in arr[]
function findLargestSumPair(arr) {
const n = arr.length;
if (n < 2) return -1;
// Initialize first and second largest element
let first, second;
if (arr[0] > arr[1]) {
first = arr[0];
second = arr[1];
} else {
first = arr[1];
second = arr[0];
}
// Traverse remaining array and find first and second
// largest elements in overall array
for (let i = 2; i < n; i++) {
// If current element is greater than first then
// update both first and second
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second) {
// If arr[i] is in between first and second then
// update second
second = arr[i];
}
}
return first + second;
}
// Driver program to test above function
const arr = [12, 34, 10, 6, 40];
console.log("Max Pair Sum is", findLargestSumPair(arr));
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem