Count the number of ways to divide N in k groups incrementally
Given two integers N and K, the task is to count the number of groups of positive integers such that their sum is N and the number of elements in each group is K.
Examples:
Input: N = 8, K = 4
Output: 5
Explanation:
Their are 5 groups such that their sum is 8 and the number of positive integers in each group is 4.
[1, 1, 1, 5], [1, 1, 2, 4], [1, 1, 3, 3], [1, 2, 2, 3], [2, 2, 2, 2]
Input: N = 24, K = 5
Output: 164
Explanation:
There are 164 such groups such that their sum is 24 and number of positive integers in each group is 5
Naive Approach: We can solve this problem using recursion. At each step of recursion put all the values greater than equal to the previous computed value.
Below is the implementation of the above approach:
C++
// C++ implementation to count the // number of ways to divide N in // groups such that each group // has K number of elements #include <bits/stdc++.h> using namespace std; // Function to count the number // of ways to divide the number N // in groups such that each group // has K number of elements int calculate(int pos, int prev, int left, int k) { // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // if N is divides completely // into less than k groups if (left == 0) return 0; int answer = 0; // put all possible values // greater equal to prev for (int i = prev; i <= left; i++) { answer += calculate(pos + 1, i, left - i, k); } return answer; } // Function to count the number of // ways to divide the number N int countWaystoDivide(int n, int k) { return calculate(0, 1, n, k); } // Driver Code int main() { int N = 8; int K = 4; cout << countWaystoDivide(N, K); return 0; } |
Time complexity: O(NK)
Efficient Approach: In the previous approach we can see that we are solving the subproblems repeatedly, i.e. it follows the property of Overlapping Subproblems. So we can memoize the same using DP table.
Below is the implementation of the above approach:
C++
// C++ implementation to count the // number of ways to divide N in // groups such that each group // has K number of elements #include <bits/stdc++.h> using namespace std; // DP Table int dp[500][500][500]; // Function to count the number // of ways to divide the number N // in groups such that each group // has K number of elements int calculate(int pos, int prev, int left, int k) { // Base Case if (pos == k) { if (left == 0) return 1; else return 0; } // if N is divides completely // into less than k groups if (left == 0) return 0; // If the subproblem has been // solved, use the value if (dp[pos][prev][left] != -1) return dp[pos][prev][left]; int answer = 0; // put all possible values // greater equal to prev for (int i = prev; i <= left; i++) { answer += calculate(pos + 1, i, left - i, k); } return dp[pos][prev][left] = answer; } // Function to count the number of // ways to divide the number N in groups int countWaystoDivide(int n, int k) { // Intialize DP Table as -1 memset(dp, -1, sizeof(dp)); return calculate(0, 1, n, k); } // Driver Code int main() { int N = 8; int K = 4; cout << countWaystoDivide(N, K); return 0; } |
Time complexity : O(N2 * K)
Recommended Posts:
- Count number of ways to divide an array into two halves with same sum
- Number of ways of distributing N identical objects in R distinct groups with no groups empty
- Count number of ways to divide a number in 4 parts
- Count ways to divide circle using N non-intersecting chord | Set-2
- Count ways to divide circle using N non-intersecting chords
- Count ways to divide C in two parts and add to A and B to make A strictly greater than B
- Number of ways of distributing N identical objects in R distinct groups
- Number of ways to divide a given number as a set of integers in decreasing order
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Find the number of ways to divide number into four parts such that a = c and b = d
- Number of ways to divide an array into K equal sum sub-arrays
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Divide 1 to n into two groups with minimum sum difference
- Divide N segments into two non-empty groups such that given condition is satisfied
- Count digits in given number N which divide N
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

