Find the largest contiguous pair sum in given Array | Java 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
import java . util . * ; class GFG { public static Vector < Integer > largestSumpair ( int [ ] arr , int n ) { Vector < Integer > pair = new Vector < Integer > ( ) ; int max_sum = Integer . MIN_VALUE , i ; for ( i = 1 ; i < n ; i ++ ) { if ( max_sum < ( arr [ i ] + arr [ i - 1 ] ) ) { max_sum = arr [ i ] + arr [ i - 1 ] ; if ( pair . isEmpty ( ) ) { pair . add ( arr [ i - 1 ] ) ; pair . add ( arr [ i ] ) ; } else { pair . set ( 0 , arr [ i - 1 ] ) ; pair . set ( 1 , arr [ i ] ) ; } } } return pair ; } 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 + " ▁ " ) ; } } }