Minimum sum subsequence such that at least one of every four consecutive elements is picked | function to calculate min sum using dp ; if elements are less than or equal to 4 ; save start four element as it is ; compute sum [ ] for all rest elements ; ( * min_element ( sum + i - 4 , sum + i ) ) ; ; Since one of the last 4 elements must be present ; driver program
function minSum ( ar , n ) { if ( n <= 4 ) return Math . min . apply ( Math , ar ) ; var i ; var sum = Array ( n ) . fill ( n ) ; sum [ 0 ] = ar [ 0 ] ; sum [ 1 ] = ar [ 1 ] ; sum [ 2 ] = ar [ 2 ] ; sum [ 3 ] = ar [ 3 ] ; for ( i = 4 ; i < n ; i ++ ) { var temp = [ ] ; var it ; for ( it = i - 4 ; it < i ; it ++ ) temp . push ( sum [ it ] ) ; sum [ i ] = ar [ i ] + Math . min . apply ( Math , temp ) ; } var temp1 = [ ] ; for ( i = n - 4 ; i < n ; i ++ ) temp1 . push ( sum [ i ] ) ; return Math . min . apply ( Math , temp1 ) ; } var ar = [ 2 , 4 , 1 , 5 , 2 , 3 , 6 , 1 , 2 , 4 ] ; var n = ar . length ; document . write ( " " + minSum ( ar , n ) ) ;