Skip to content
Related Articles

Related Articles

Improve Article
Find the largest pair sum in an unsorted array
  • Difficulty Level : Easy
  • Last Updated : 01 Jun, 2021

Given an unsorted of distinct integers, find the largest pair sum in it. For example, the largest pair sum in {12, 34, 10, 6, 40} is 74.
Difficulty Level: Rookie 
Expected Time Complexity: O(n) [Only one traversal of array is allowed]
 

This problem mainly boils down to finding the largest and second largest element in array. We can find the largest and second largest in O(n) time by traversing array once. 
 

1) Initialize the 
   first = Integer.MIN_VALUE
   second =  Integer.MIN_VALUE
2) Loop through the elements
   a) 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. 
3) Return (first + second)

Below is the implementation of above algorithm: 
 

C++




// C++ program to find largest pair sum in a given array
#include<iostream>
using namespace std;
 
/* Function to return largest pair sum. Assumes that
   there are at-least  two elements in arr[] */
int findLargestSumPair(int arr[], int n)
{
    // 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 && arr[i] != first)
            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]);
    cout << "Max Pair Sum is " << findLargestSumPair(arr, n);
    return 0;
}

Java




public class LargestPairSum {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
 
        int arr[] = {1,2,3,4,9,10,5,6,7,8};
        System.out.println(largestPairSum(arr,arr.length));
    }
 
    private static int largestPairSum(int[] arr, int n) {
         
        int max1 = Integer.MIN_VALUE;
        int max2 = Integer.MIN_VALUE;
         
        for(int i=0;i<n;i++) {
            if(arr[i]>max1) {
                max2 = max1;
                max1 = arr[i];
            }
        }
        return max1+max2;
    }
 
}

Python3




# Python3 program to find largest
# pair sum in a given array
 
# Function to return largest pair
# sum. Assumes that there are
# at-least two elements in arr[]
def findLargestSumPair(arr, n):
 
    # Initialize first and second
    # largest element
    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 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]
         
 
        # If arr[i] is in between first
        # and second then update second
        elif arr[i] > second and arr[i] != first:
            second = arr[i]
     
    return (first + second)
 
# Driver program to test above function */
arr = [12, 34, 10, 6, 40]
n = len(arr)
print("Max Pair Sum is",
      findLargestSumPair(arr, n))
 
# This code is contributed by Smitha Dinesh Semwal

C#




// C# program to find largest
// pair sum in a given array
using System;
 
class GFG
{
    /* Method to return largest pair
    sum. Assumes that there are
    at-least two elements in arr[] */
    static int findLargestSumPair(int []arr)
    {
        // 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 < arr.Length; 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 &&
                     arr[i] != first)
                second = arr[i];
        }
        return (first + second);
    }
    // Driver Code
    public static void Main()
    {
        int []arr1 = new int[]{12, 34, 10, 6, 40};
        Console.Write("Max Pair Sum is " +
                       findLargestSumPair(arr1));
         
    }
}

PHP




<?php
// PHP program to find largest
// pair sum in a given array
 
// Function to return largest
// pair sum. Assumes that
// there are at-least two
// elements in arr[] */
function findLargestSumPair($arr, $n)
{
     
    // Initialize first and
    // second largest element
    $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 ( $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 and
                 $arr[$i] != $first)
            $second = $arr[$i];
    }
    return ($first + $second);
}
 
    // Driver Code
    $arr = array(12, 34, 10, 6, 40);
    $n = count($arr);
    echo "Max Pair Sum is "
          , findLargestSumPair($arr, $n);
 
// This code is contributed by anuj_67.
?>

Javascript




<script>
 
    // Javascript program to find largest
    // pair sum in a given array
     
    /* Method to return largest pair
    sum. Assumes that there are
    at-least two elements in arr[] */
    function findLargestSumPair(arr)
    {
        // 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 < arr.length; 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 &&
                     arr[i] != first)
                second = arr[i];
        }
        return (first + second);
    }
       
    let arr1 = [12, 34, 10, 6, 40];
    document.write("Max Pair Sum is " + findLargestSumPair(arr1));
     
    // This code is contributed by divyeshrabadiy07.
</script>

Output : 

19

Time complexity of above solution is O(n).
Space complexity of above solution is O(1).



This article is contributed by Rishabh and improved by Akshita Patel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :