Maximum sum subsequence with at | PHP program to find maximum sum subsequence such that elements are at least k distance away . ; We fill MS from right to left . ; Driver code
< ? php function maxSum ( $ arr , $ N , $ k ) { $ MS [ $ N - 1 ] = $ arr [ $ N - 1 ] ; for ( $ i = $ N - 2 ; $ i >= 0 ; $ i -- ) { if ( $ i + $ k + 1 >= $ N ) $ MS [ $ i ] = max ( $ arr [ $ i ] , $ MS [ $ i + 1 ] ) ; else $ MS [ $ i ] = max ( $ arr [ $ i ] + $ MS [ $ i + $ k + 1 ] , $ MS [ $ i + 1 ] ) ; } return $ MS [ 0 ] ; } $ N = 10 ; $ k = 2 ; $ arr = array ( 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 ) ; echo ( maxSum ( $ arr , $ N , $ k ) ) ; ? >