Find the smallest contiguous sum pair in an Array | C ++ program to find the smallest sum contiguous pair ; Function to find the smallest sum contiguous pair ; Contiguous pair ; Initialize minimum sum with maximum value ; Checking for minimum value ; Add to pair ; Updating pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > smallestSumpair ( int arr [ ] , int n ) { vector < int > pair ; int min_sum = INT_MAX ; for ( int i = 1 ; i < n ; i ++ ) { if ( min_sum > ( arr [ i ] + arr [ i - 1 ] ) ) { min_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 [ ] = { 4 , 9 , -3 , 2 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; vector < int > pair = smallestSumpair ( arr , n ) ; cout << pair [ 0 ] << " ▁ " << pair [ 1 ] ; }