Find the largest contiguous pair sum in given Array | C ++ program to find the a contiguous pair from the which has the largest sum ; Function to find and return the largest sum contiguous pair ; Stores the contiguous pair ; Initialize maximum sum ; Compare sum of pair with max_sum ; Insert the pair ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > largestSumpair ( int arr [ ] , int n ) { vector < int > pair ; int max_sum = INT_MIN , i ; for ( i = 1 ; i < n ; i ++ ) { if ( max_sum < ( arr [ i ] + arr [ i - 1 ] ) ) { max_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . empty ( ) ) { pair . push_back ( arr [ i - 1 ] ) ; pair . push_back ( arr [ i ] ) ; } else { pair [ 0 ] = arr [ i - 1 ] ; pair [ 1 ] = arr [ i ] ; } } } return pair ; } int 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 ; }