Number of ways to split N as sum of K numbers from the given range | Javascript implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] ; Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] ; Base Case ; if N is divides completely into less than k groups ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
var mod = 1000000007 ; function calculate ( pos , left , k , L , R ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; var answer = 0 ; for ( var i = L ; i <= R ; i ++ ) { if ( i > left ) break ; answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod ; } return answer ; } function countWaystoDivide ( n , k , L , R ) { return calculate ( 0 , n , k , L , R ) ; } var N = 12 ; var K = 3 ; var L = 1 ; var R = 5 ; document . write ( countWaystoDivide ( N , K , L , R ) ) ;