VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-largest-pair-sum-in-an-unsorted-array/

⇱ Largest pair sum in an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Largest pair sum in an array

Last Updated : 3 Feb, 2026

Given an unsorted array arr[] of distinct integers, find the largest pair sum in it. If there are less than 2 elements, then we need to return -1.

Input : arr[] = [12, 34, 10, 6, 40]
Output : 74
Explanation : The two largest elements are 40 and 34. Their sum is 74.

Input : arr[] = [10, 10, 10]
Output : 20
Explanation : Any two 10's give the maximum sum.

Input arr[] = [10]
Output : -1
Explanation : At least two elements are required to form a pair.

[Naive Approach] Using Nested Loops - O(n2) 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.


Output
Max Pair Sum is 74

[Expected Approach] Largest two Elements - O(n) Time and O(1) Space

The largest pair sum in an array is always formed by the largest and second-largest elements. Instead of checking all possible pairs, we traverse the array once and keep track of these two values. Whenever a larger element is found, we update the largest and second-largest accordingly. Finally, their sum gives the required maximum pair sum.


Output
Max Pair Sum is 74
Comment
Article Tags:
Article Tags: