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Â
Maximum 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:Â
Â
- Generate all the continuous pairs one by one and calculate their sum.
- Compare the sum of every pair with the maximum sum and update the pair corresponding to the maximum sum accordingly.
- Return the pair representing the maximum sum.
Below is the implementation of the above approach :
Â
C++
// C++ program to find the// a contiguous pair from the// which has the largest sum#include <bits/stdc++.h>using namespace std;Â
// Function to find and return// the largest sum contiguous pairvector<int> largestSumpair(int arr[], int n){Â
    // Stores the contiguous pair    vector<int> pair;Â
    // Initialize 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[0] = arr[i - 1];                pair[1] = arr[i];            }        }    }    return pair;}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 11, -5, 9, -3, 2 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â
    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
// Java program to find the// a contiguous pair from the// which has the largest sumimport java.util.*;Â
class GFG {Â
    // Function to find and return    // the largest sum contiguous pair    public static Vector<Integer> largestSumpair(int[] arr,                                                 int n)    {Â
        // Stores the contiguous pair        Vector<Integer> pair = new Vector<Integer>();Â
        // Initialize 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<Integer> pair = new Vector<Integer>();        pair = largestSumpair(arr, N);        for (Integer it : pair) {            System.out.print(it + " ");        }    }}Â
// This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program to find the# a contiguous pair from the# which has the largest sumÂ
# importing sysimport sysÂ
# Function to find and return# the largest sum contiguous pairÂ
Â
def largestSumpair(arr, n):    # Stores the contiguous pair    pair = []Â
# Initialize 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 Codearr = [11, -5, 9, -3, 2]N = len(arr)pair = largestSumpair(arr, N)print(pair[0], end=" ")print(pair[1], end=" ") |
C#
// C# program to find the// a contiguous pair from the// which has the largest sumusing 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();Â
        // Initialize 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[0] = arr[i - 1];                    pair[1] = 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 |
Javascript
<script>// Javascript program to find the// a contiguous pair from the// which has the largest sumÂ
// Function to find and return// the largest sum contiguous pairfunction largestSumpair(arr, n){Â
    // Stores the contiguous pair    var pair = [];Â
    // Initialize maximum sum    var max_sum = Number.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.length != 0) {Â
                // Insert the pair                pair[0] = arr[i - 1];                pair[1]= arr[i];            }            else {                pair[0] = arr[i - 1];                pair[1] = arr[i];            }        }    }    return pair;}Â
// Driver CodeÂ
var arr = [ 11, -5, 9, -3, 2 ];var N = arr.length;Â
pair = largestSumpair(arr, N);for (var it of pair) {Â Â Â Â document.write(it + " ");}Â
// This code is contributed by shubhamsingh10</script> |
11 -5
Â
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
