Number of ways to cut a stick of length N into in even length at most K units long pieces | Recursive Function to count the total number of ways ; Base case if no - solution exist ; Condition if a solution exist ; Check if already calculated ; Initialize counter ; Recursive call ; Store the answer ; Return the answer ; Driver Code
function solve ( n , k , mod , dp ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; let cnt = 0 ; for ( let i = 2 ; i <= k ; i += 2 ) { cnt = ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ; } dp [ n ] = cnt ; return cnt ; } let mod = ( 1e9 + 7 ) ; let n = 4 , k = 2 ; let dp = new Array ( n + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; let ans = solve ( n , k , mod , dp ) ; document . write ( ans ) ;