Maximum subsequence sum such that no three are consecutive | JavaScript program to find the maximum sum such that no three are consecutive using recursion . ; Returns maximum subsequence sum such that no three elements are consecutive ; Base cases ( process first three elements ) ; Process rest of the elements We have three cases ; Driver code
let arr = [ 100 , 1000 , 100 , 1000 , 1 ] ; let sum = new Array ( 10000 ) ; for ( let i = 0 ; i < 10000 ; i ++ ) { sum [ i ] = - 1 ; } function maxSumWO3Consec ( n ) { if ( sum [ n ] != - 1 ) { return sum [ n ] ; } if ( n == 0 ) { return sum [ n ] = 0 ; } if ( n == 1 ) { return sum [ n ] = arr [ 0 ] ; } if ( n == 2 ) { return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; } return sum [ n ] = 500 + Math . max ( Math . max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n ] ) , arr [ n ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) ; } let n = arr . length - 1 ; document . write ( maxSumWO3Consec ( n ) + 1 ) ;