Count the number of ways to divide N in k groups incrementally | Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; 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
function calculate ( pos , prev , left , k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; let answer = 0 ; for ( let i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return answer ; } function countWaystoDivide ( n , k ) { return calculate ( 0 , 1 , n , k ) ; } let N = 8 ; let K = 4 ; document . write ( countWaystoDivide ( N , K ) ) ;