Find minimum sum such that one of every three consecutive elements is taken | function to find minimum of 3 elements ; Returns minimum possible sum of elements such that an element out of every three consecutive elements is picked . ; Create a DP table to store results of subproblems . sum [ i ] is going to store minimum possible sum when arr [ i ] is part of the solution . ; When there are less than or equal to 3 elements ; Iterate through all other elements ; Driver code
< ? php function minimum ( $ a , $ b , $ c ) { return min ( min ( $ a , $ b ) , $ c ) ; } function findMinSum ( $ arr , $ n ) { $ sum [ $ n ] = 0 ; $ sum [ 0 ] = $ arr [ 0 ] ; $ sum [ 1 ] = $ arr [ 1 ] ; $ sum [ 2 ] = $ arr [ 2 ] ; for ( $ i = 3 ; $ i < $ n ; $ i ++ ) $ sum [ $ i ] = $ arr [ $ i ] + minimum ( $ sum [ $ i - 3 ] , $ sum [ $ i - 2 ] , $ sum [ $ i - 1 ] ) ; return minimum ( $ sum [ $ n - 1 ] , $ sum [ $ n - 2 ] , $ sum [ $ n - 3 ] ) ; } $ arr = array ( 1 , 2 , 3 , 20 , 2 , 10 , 1 ) ; $ n = sizeof ( $ arr ) ; echo " Min ▁ Sum ▁ is ▁ " , findMinSum ( $ arr , $ n ) ; ? >