Minimize the sum of pair which upon removing divides the Array into 3 subarrays | Java implementation of the above approach ; Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Driver Code ; Given array
class GFG { static int minSumPair ( int arr [ ] , int N ) { if ( N < 5 ) { return - 1 ; } int [ ] prefixMin = new int [ N ] ; prefixMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = Math . min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } int ans = Integer . MAX_VALUE ; for ( int i = 3 ; i < N - 1 ; i ++ ) { ans = Math . min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 4 , 6 , 3 , 7 } ; int N = arr . length ; System . out . print ( minSumPair ( arr , N ) + "NEW_LINE"); } }