Find the smallest contiguous sum pair in an Array | Java program to find the smallest sum contiguous pair ; Function to find the smallest sum contiguous pair ; Stores the contiguous pair ; Initialize minimum sum ; Checking for minimum value ; Add to pair ; Updating pair ; Driver Code
import java . util . * ; class GFG { public static Vector < Integer > smallestSumpair ( int [ ] arr , int n ) { Vector < Integer > pair = new Vector < Integer > ( ) ; int min_sum = Integer . MAX_VALUE , i ; for ( i = 1 ; i < n ; i ++ ) { if ( min_sum > ( arr [ i ] + arr [ i - 1 ] ) ) { min_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 [ ] = { 4 , 9 , - 3 , 2 , 0 } ; int N = arr . length ; Vector < Integer > pair = new Vector < Integer > ( ) ; pair = smallestSumpair ( arr , N ) ; System . out . println ( pair . get ( 0 ) + " ▁ " + pair . get ( 1 ) ) ; } }