Ways to sum to N using array elements with repetition allowed | JavaScript implementation to count ways to sum up to a given value N ; method to count the total number of ways to sum up to ' N ' ; base case ; count ways for all values up to ' N ' and store the result ; if i >= arr [ j ] then accumulate count for value ' i ' as ways to form value ' i - arr [ j ] ' ; required number of ways ; Driver code
let arr = [ 1 , 5 , 6 ] ; function countWays ( N ) { let count = new Array ( N + 1 ) ; count . fill ( 0 ) ; count [ 0 ] = 1 ; for ( let i = 1 ; i <= N ; i ++ ) for ( let j = 0 ; j < arr . length ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; } let N = 7 ; document . write ( " " + countWays ( N ) ) ;