Number of ways to obtain each numbers in range [ 1 , b + c ] by adding any two numbers in range [ a , b ] and [ b , c ] | Javascript program to calculate the number of ways ; 2 is added because sometimes we will decrease the value out of bounds . ; Initialising the array with zeros . You can do using memset too . ; Printing the array ; Driver code
function CountWays ( a , b , c ) { let x = b + c + 2 ; let arr = new Array ( x ) ; arr . fill ( 0 ) ; for ( let i = 1 ; i <= b ; i ++ ) { arr [ i + b ] ++ ; arr [ i + c + 1 ] -- ; } for ( let i = 1 ; i < x - 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; document . write ( arr [ i ] + " " ) ; } document . write ( " " ) ; } let a = 1 ; let b = 2 ; let c = 2 ; CountWays ( a , b , c ) ;