# Find the largest contiguous pair sum in given Array

Given an integer array arr[] of size N, the task is to find contiguous pair {a, b} such that sum of both elements in the pair is maximum. If there are more than one such pairs with maximum sum then print any of such pair. In the case of multiple pairs with the largest sum, print any one of them.
Examples:

Input: arr[] = {1, 2, 3, 4}
Output: 3 4
Explanation:
Here, the contiguous pairs in the array are:
{1, 2} -> Sum 3
{2, 3} -> Sum 5
{3, 4} -> Sum 7
Maxiumum sum is 7 for the pair is (3, 4) so this is answer.

Input: arr[] = {11, -5, 9, -3, 2}
Output: 11 -5
The contiguous pairs with their respective sums are :
{11, -5} -> Sum 6
{-5, 9} -> Sum 4
{9, -3} -> Sum 6
{-3, 2} -> Sum -1
The maximum sum obtained is 6 from the pairs (11, -5) and (9, -3).

Approach:
Follow the steps below to solve the problem:

1. Generate all the continuous pairs one by one and calculate there sum.
2. Compare the sum of every pair with the maximum sum and update the pair corresponding to the maximum sum accordingly.
3. Return the pair representing the maximum sum.

Below is the implementation of the above approach :

 `// C++ program to find the  ` `// a contiguous pair from the ` `// which has the largest sum ` `#include ` `using` `namespace` `std; ` ` `  `// Function to find and return ` `// the largest sum contiguous pair ` `vector<``int``> largestSumpair(``int` `arr[], ``int` `n) ` `{ ` `     `  `    ``// Stores the contiguous pair  ` `    ``vector<``int``> pair; ` `     `  `    ``// Intialize maximum sum ` `    ``int` `max_sum = INT_MIN, i; ` `     `  `    ``for``(i = 1; i < n; i++)  ` `    ``{ ` `         `  `        ``// Compare sum of pair with max_sum ` `        ``if` `(max_sum < (arr[i] + arr[i - 1])) ` `        ``{ ` `            ``max_sum = arr[i] + arr[i - 1]; ` `            ``if` `(pair.empty())  ` `            ``{  ` `                 `  `                ``// Insert the pair ` `                ``pair.push_back(arr[i - 1]); ` `                ``pair.push_back(arr[i]); ` `            ``} ` `            ``else` `            ``{ ` `                ``pair = arr[i - 1]; ` `                ``pair = arr[i]; ` `            ``} ` `        ``} ` `        ``return` `pair; ` `    ``} ` `} ` `     `  `// Driver Code  ` `int` `main()  ` `{  ` `    ``int` `arr[] = {11, -5, 9, -3, 2}; ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr); ` `     `  `    ``vector<``int``> pair = largestSumpair(arr, N); ` `    ``for``(``auto` `it = pair.begin(); it != pair.end(); ++it) ` `    ``{ ` `        ``cout << *it << ``' '``;  ` `    ``} ` `     `  `    ``return` `0;  ` `}  ` ` `  `// This code is contributed by shubhamsingh10 `

 `// Java program to find the  ` `// a contiguous pair from the  ` `// which has the largest sum  ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `// Function to find and return  ` `// the largest sum contiguous pair  ` `public` `static` `Vector largestSumpair(``int``[] arr, ` `                                             ``int` `n)  ` `{  ` `     `  `    ``// Stores the contiguous pair  ` `    ``Vector pair = ``new` `Vector();  ` `     `  `    ``// Intialize maximum sum  ` `    ``int` `max_sum = Integer.MIN_VALUE, i;  ` `     `  `    ``for``(i = ``1``; i < n; i++)  ` `    ``{  ` `         `  `        ``// Compare sum of pair with max_sum  ` `        ``if` `(max_sum < (arr[i] + arr[i - ``1``]))  ` `        ``{  ` `            ``max_sum = arr[i] + arr[i - ``1``];  ` `             `  `            ``if` `(pair.isEmpty())  ` `            ``{  ` `                 `  `                ``// Insert the pair  ` `                ``pair.add(arr[i - ``1``]);  ` `                ``pair.add(arr[i]);  ` `            ``}  ` `            ``else` `            ``{  ` `                ``pair.set(``0``, arr[i - ``1``]); ` `                ``pair.set(``1``, arr[i]); ` `            ``}  ` `        ``}  ` `    ``}  ` `    ``return` `pair; ` `}  ` ` `  `// Driver Code     ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `arr[] = { ``11``, -``5``, ``9``, -``3``, ``2` `};  ` `    ``int` `N = arr.length;  ` `     `  `    ``Vector pair = ``new` `Vector();  ` `    ``pair = largestSumpair(arr, N); ` `    ``for` `(Integer it : pair) ` `    ``{          ` `        ``System.out.print(it + ``" "``); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by divyeshrabadiya07 `

 `# Python3 program to find the  ` `# a contiguous pair from the ` `# which has the largest sum ` ` `  `# importing sys ` `import` `sys ` ` `  `# Function to find and return ` `# the largest sum contiguous pair ` `def` `largestSumpair(arr, n): ` `# Stores the contiguous pair  ` `    ``pair ``=` `[] ` ` `  `# Intialize maximum sum ` `    ``max_sum ``=` `-``sys.maxsize``-``1` ` `  `    ``for` `i ``in` `range``(``1``, n): ` `     `  `        ``# Compare sum of pair with max_sum ` `        ``if` `max_sum < ( arr[i] ``+` `arr[i``-``1``] ): ` `            ``max_sum ``=` `arr[i] ``+` `arr[i``-``1``] ` `         `  `            ``if` `pair ``=``=` `[]: ` `                ``# Insert the pair ` `                ``pair.append(arr[i``-``1``]) ` `                ``pair.append(arr[i]) ` `            ``else``: ` `                ``pair[``0``] ``=` `arr[i``-``1``] ` `                ``pair[``1``] ``=` `arr[i] ` ` `  `    ``return` `pair ` `     `  `     `  `# Driver Code  ` `arr ``=` `[``11``, ``-``5``, ``9``, ``-``3``, ``2``]  ` `N ``=` `len``(arr)  ` `pair ``=` `largestSumpair(arr, N) ` `print``(pair[``0``], end ``=``" "``) ` `print``(pair[``1``], end ``=``" "``) `

 `// C# program to find the  ` `// a contiguous pair from the  ` `// which has the largest sum  ` `using` `System; ` `using` `System.Collections;  ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` ` `  `// Function to find and return  ` `// the largest sum contiguous pair  ` `public` `static` `ArrayList largestSumpair(``int``[] arr, ` `                                       ``int` `n)  ` `{  ` `     `  `    ``// Stores the contiguous pair  ` `    ``ArrayList pair = ``new` `ArrayList();  ` `     `  `    ``// Intialize maximum sum  ` `    ``int` `max_sum = ``int``.MinValue, i;  ` `     `  `    ``for``(i = 1; i < n; i++)  ` `    ``{  ` `         `  `        ``// Compare sum of pair with max_sum  ` `        ``if` `(max_sum < (arr[i] + arr[i - 1]))  ` `        ``{  ` `            ``max_sum = arr[i] + arr[i - 1];  ` `             `  `            ``if` `(pair.Count == 0)  ` `            ``{  ` `                 `  `                ``// Insert the pair  ` `                ``pair.Add(arr[i - 1]);  ` `                ``pair.Add(arr[i]);  ` `            ``}  ` `            ``else` `            ``{  ` `                ``pair = arr[i - 1]; ` `                ``pair = arr[i]; ` `            ``}  ` `        ``}  ` `    ``}  ` `    ``return` `pair; ` `}  ` ` `  `// Driver code ` `public` `static` `void` `Main(``string``[] args) ` `{ ` `    ``int` `[]arr = { 11, -5, 9, -3, 2 };  ` `    ``int` `N = arr.Length;  ` `     `  `    ``ArrayList pair = ``new` `ArrayList();  ` `    ``pair = largestSumpair(arr, N); ` `     `  `    ``foreach``(``int` `it ``in` `pair) ` `    ``{          ` `        ``Console.Write(it + ``" "``); ` `    ``} ` `} ` `} ` ` `  `// This code is contributed by rutvik_56 `

Output:
```11 -5
```

Time Complexity: O(N)
Auxiliary Space: O(1)

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.

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :