Minimize the sum of pair which upon removing divides the Array into 3 subarrays | Function to find minimum possible sum of pair which breaks the array into 3 non - empty subarrays ; prefixMin [ i ] contains minimum element till i ; Given array
function minSumPair ( arr , N ) { if ( N < 5 ) { return - 1 ; } let prefixMin = new Array ( N ) . fill ( 0 ) ; prefixMin [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < N - 1 ; i ++ ) { prefixMin [ i ] = Math . min ( arr [ i ] , prefixMin [ i - 1 ] ) ; } let ans = Number . MAX_VALUE ; for ( let i = 3 ; i < N - 1 ; i ++ ) { ans = Math . min ( ans , arr [ i ] + prefixMin [ i - 2 ] ) ; } return ans ; } let arr = [ 5 , 2 , 4 , 6 , 3 , 7 ] ; let N = arr . length ; document . write ( minSumPair ( arr , N ) ) ;