Count number of ways to partition a set into k subsets
Last Updated :
18 Nov, 2024
Given two numbers n and k where n represents a number of elements in a set, find a number of ways to partition the set into k subsets.
Example:
Input: n = 3, k = 2
Output: 3
Explanation: Let the set be [1, 2, 3], we can partition it into 3 subsets in the following ways [[1,2], [3]], [[1], [2,3]], [[1,3], [2]].
Input: n = 3, k = 1
Output: 1
Explanation: There is only one way [[1, 2, 3]].
Using Recursion - O(2^n) Time and O(n) Space
We can recursively calculate the number of ways to partition a set of n elements by considering each element and either placing it in an existing subset or creating a new subset. For each element, we calculate the number of ways to partition the remaining n-1 elements into k subsets and then sum these values for all possible k. This gives us the following recurrence relation for Stirling numbers of the second kind:
- The previous n – 1 elements are divided into k partitions, i.e S(n-1, k) ways. Put this nth element into one of the previous k partitions. So, count = k * S(n-1, k)
- The previous n – 1 elements are divided into k – 1 partitions, i.e S(n-1, k-1) ways. Put the nth element into a new partition ( single element partition). So, count = S(n-1, k-1)
Base case: Return 0 for invalid cases (n == 0, k == 0, or k > n) and 1 for trivial cases (k == 1 or k == n).
Total count = S(n, k) = k * S( n - 1, k) + S(n - 1, k - 1)
C++
// A C++ program to count number of partitions
// of a set with n elements into k subsets
#include<iostream>
using namespace std;
// Returns count of different partitions of n
// elements in k subsets
int countP(int n, int k) {
// Base cases
if (n == 0 || k == 0 || k > n)
return 0;
if (k == 1 || k == n)
return 1;
// Recursive formula
// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k*countP(n-1, k) + countP(n-1, k-1);
}
int main() {
int n = 5, k = 2;
cout << countP(n, k);
return 0;
}
Java
// A Java program to count number of partitions
// of a set with n elements into k subsets
class GfG {
// Returns count of different partitions of n
// elements in k subsets
static int countP(int n, int k) {
// Base cases
if (n == 0 || k == 0 || k > n)
return 0;
if (k == 1 || k == n)
return 1;
// Recursive formula
// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k * countP(n - 1, k) + countP(n - 1, k - 1);
}
public static void main(String[] args) {
int n = 5, k = 2;
System.out.println(countP(n, k));
}
}
Python
# A Python program to count number of partitions
# of a set with n elements into k subsets
# Returns count of different partitions of n
# elements in k subsets
def countP(n, k):
# Base cases
if n == 0 or k == 0 or k > n:
return 0
if k == 1 or k == n:
return 1
# Recursive formula
# S(n+1, k) = k*S(n, k) + S(n, k-1)
return k * countP(n - 1, k) + countP(n - 1, k - 1)
n = 5
k = 2
print(countP(n, k))
C#
// A C# program to count number of partitions
// of a set with n elements into k subsets
using System;
class GfG {
// Memoization table to store subproblem results
static int[,] memo;
// Returns count of different partitions of
// n elements in k subsets
static int countP(int n, int k) {
// Base cases
if (n == 0 || k == 0 || k > n) {
return 0;
}
if (k == 1 || k == n) {
return 1;
}
// If the value is already computed,
// return it
if (memo[n, k] != -1) {
return memo[n, k];
}
memo[n, k] = k * countP(n - 1, k) + countP(n - 1, k - 1);
return memo[n, k];
}
static void Main(string[] args) {
// Set the values for n and k
int n = 5, k = 2;
// Initialize memoization table with -1
// (indicating not computed yet)
// Adjusted memoization size to [n+1, n+1]
memo = new int[n + 1, n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
memo[i, j] = -1;
}
}
// Call the function and
// print the result
Console.WriteLine(countP(n, k));
}
}
JavaScript
// A JavaScript program to count number of partitions
// of a set with n elements into k subsets
// Returns count of different partitions of n
// elements in k subsets
function countP(n, k) {
// Base cases
if (n === 0 || k === 0 || k > n) {
return 0;
}
if (k === 1 || k === n) {
return 1;
}
// Recursive formula
// S(n+1, k) = k*S(n, k) + S(n, k-1)
return k * countP(n - 1, k) + countP(n - 1, k - 1);
}
let n = 5, k = 2;
console.log(countP(n, k));
Using Top-Down DP (Memoization) - O(n * k) Time and O(n * k) Space
If we notice carefully, we can observe that the above recursive solution holds the following two properties of Dynamic Programming:
1. Optimal Substructure: The number of ways to partition a set of n elements into k subsets depends on two smaller subproblems:
- The number of ways to partition the first n-1 elements into k subsets, and
- The number of ways to partition the first n-1 elements into k-1 subsets and then add the new element as its own subset. By combining these optimal solutions, we can efficiently calculate the total number of ways to partition the set.
2. Overlapping Subproblems: In the recursive approach, certain subproblems are recalculated multiple times. For example, when computing S(n, k), the subproblems S(n-1, k) and S(n-1, k-1) are recomputed multiple times. This redundancy leads to overlapping subproblems, which can be avoided using memoization or tabulation. Below is recursion tree of countP(10, 7). The subproblem countP(8,6) or CP(8,6) is called multiple times.

- The recursive solution involves two parameters: n (the number of elements) and k (the number of subsets).
- We create a 2D memoization table of size (n + 1) x (k + 1) to store results for all combinations of n and k.
- We initialize the table with -1 to indicate uncomputed subproblems, we check if the value at memo[n][k] is -1. If it is, we proceed to compute the result. otherwise, we return the stored result.
C++
// C++ code of finding the bellNumber
// using Memoization
#include <iostream>
#include <vector>
using namespace std;
// Function to compute Stirling numbers of
// the second kind S(n, k) with memoization
int stirling(int n, int k, vector<vector<int>>& memo) {
// Base cases
if (n == 0 && k == 0) return 1;
if (k == 0 || n == 0) return 0;
if (n == k) return 1;
if (k == 1) return 1;
// Check if result is already computed
if (memo[n][k] != -1) return memo[n][k];
// Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +
stirling(n - 1, k - 1, memo);
return memo[n][k];
}
// Function to calculate the total number of
// ways to partition a set of `n` elements
int countP(int n, int k) {
// Initialize memoization table with -1
vector<vector<int>> memo(n + 1, vector<int>(n + 1, -1));
return stirling(n, k, memo);
}
int main() {
int n = 5, k = 2;
int result = countP(n, k);
cout << result << endl;
return 0;
}
Java
// Java code to find the total subsets
// using Memoization
class GfG {
// Function to compute Stirling numbers of
// the second kind S(n, k) with memoization
static int stirling(int n, int k, int[][] memo) {
// Base cases
if (n == 0 && k == 0) return 1;
if (k == 0 || n == 0) return 0;
if (n == k) return 1;
if (k == 1) return 1;
// Check if result is already computed
if (memo[n][k] != -1) return memo[n][k];
// Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +
stirling(n - 1, k - 1, memo);
return memo[n][k];
}
// Function to calculate the total number of
// ways to partition a set of `n` elements
static int countP(int n, int k) {
// Initialize memoization table with -1
int[][] memo = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
memo[i][j] = -1;
}
}
return stirling(n, k, memo);
}
public static void main(String[] args) {
int n = 5, k = 2;
int result = countP(n, k);
System.out.println(result);
}
}
Python
# Python code to find the total subsets
# using Memoization
# Function to compute Stirling numbers of
# the second kind S(n, k) with memoization
def stirling(n, k, memo):
# Base cases
if n == 0 and k == 0:
return 1
if k == 0 or n == 0:
return 0
if n == k:
return 1
if k == 1:
return 1
# Check if result is already computed
if memo[n][k] != -1:
return memo[n][k]
# Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +\
stirling(n - 1, k - 1, memo)
return memo[n][k]
# Function to calculate the total number of
# ways to partition a set of `n` elements
def countP(n, k):
# Initialize memoization table with -1
memo = [[-1 for _ in range(n + 1)] for _ in range(n + 1)]
return stirling(n, k, memo)
if __name__ == "__main__":
n = 5
k = 2
result = countP(n, k)
print(result)
C#
// C# code to find the total subsets
// using Memoization
using System;
class GfG {
// Function to compute Stirling numbers of the
// second kind S(n, k) with memoization
static int Stirling(int n, int k, int[,] memo) {
// Base cases
if (n == 0 && k == 0) return 1;
if (k == 0 || n == 0) return 0;
if (n == k) return 1;
if (k == 1) return 1;
// Check if result is already computed
if (memo[n, k] != -1) return memo[n, k];
// Recursive formula
memo[n, k] = k * Stirling(n - 1, k, memo)
+ Stirling(n - 1, k - 1, memo);
return memo[n, k];
}
// Function to calculate the total number of ways
// to partition a set of `n` elements into `k` subsets
static int CountP(int n, int k) {
// Initialize memoization table with -1
int[,] memo = new int[n + 1, n + 1];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
memo[i, j] = -1;
}
}
return Stirling(n, k, memo);
}
static void Main() {
int n = 5, k = 2;
int result = CountP(n, k);
Console.WriteLine(result);
}
}
JavaScript
// JavaScript code to find the total subsets
// using Memoization
// Function to compute Stirling numbers of
// the second kind S(n, k) with memoization
function stirling(n, k, memo) {
// Base cases
if (n === 0 && k === 0) return 1;
if (k === 0 || n === 0) return 0;
if (n === k) return 1;
if (k === 1) return 1;
// Check if result is already computed
if (memo[n][k] !== -1) return memo[n][k];
// Recursive formula
memo[n][k] = k * stirling(n - 1, k, memo) +
stirling(n - 1, k - 1, memo);
return memo[n][k];
}
// Function to calculate the total number of
// ways to partition a set of `n` elements
function countP(n, k) {
// Initialize memoization table with -1
const memo = Array.from({ length: n + 1 }, () => Array(n + 1).fill(-1));
return stirling(n, k, memo);
}
const n = 5, k = 2;
const result = countP(n, k);
console.log(result);
Using Bottom-Up DP (Tabulation) - O(n * k) Time and O(n * k) Space
The approach is similar to the previous one, but instead of solving the problem recursively, we can calculate the solution using a bottom-up dynamic programming approach. We maintain a 2D table dp[][]where dp[i][j] represents the number of ways to partition a set of i elements into j subsets.
Base Case:
- For i = 0 and 0 <= j < n, dp[0][j] = 0 (no elements to partition into subsets).
- For j = 0 and 0 <= i < n, dp[i][0] = 0(cannot partition i elements into 0 subsets).
- For i = j, dp[i][j] = 1(one way to partition i elements into i subsets).
Recursive Case:
- For i > 1 and j > 1,
- dp[i][j] = j * dp[i-1][j] + dp[i-1][j - 1]
- This formula arises from considering two options: either placing the i-th element in one of the existing subsets (which is j * dp[i-1][j]) or placing it in a new subset (dp[i-1][j-1]).
C++
// A Dynamic Programming based C++ program to count
// number of partitions of a set with n elements
#include <iostream>
using namespace std;
// Returns count of different partitions
// of n elements in k subsets
int countP(int n, int k) {
// Table to store results of subproblems
int dp[n + 1][k + 1];
// Initialize base cases
for (int i = 0; i <= n; i++)
// No way to partition n elements
// into 0 subsets if n > 0
dp[i][0] = 0;
for (int i = 0; i <= k; i++)
// 1 way to partition 0 elements
// into 0 subsets
dp[0][i] = (i == 0) ? 1 : 0;
// Fill rest of the entries in
// dp[][] in bottom up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || i == j)
dp[i][j] = 1;
else
dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
}
}
return dp[n][k];
}
int main() {
int n = 5, k = 2;
cout << countP(n, k);
return 0;
}
Java
// A Dynamic Programming based Java program to count
// number of partitions of a set with n elements
class GfG {
// Returns count of different partitions
// of n elements in k subsets
static int countP(int n, int k) {
// Table to store results
// of subproblems
int[][] dp = new int[n + 1][k + 1];
// Initialize base cases
for (int i = 0; i <= n; i++) {
// No way to partition n elements
// into 0 subsets if n > 0
dp[i][0] = 0;
}
// 1 way to partition 0 elements
// into 0 subsets
dp[0][0] = 1;
// Fill rest of the entries in dp[][]
// in bottom-up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (j == 1 || i == j)
// One way to partition i elements
// into i subsets or 1 subset
dp[i][j] = 1;
else
dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
}
}
return dp[n][k];
}
public static void main(String[] args) {
int n = 5, k = 2;
System.out.println(countP(n, k));
}
}
Python
# A Dynamic Programming based Python program to count
# number of partitions of a set with n elements
# Returns count of different partitions
# of n elements in k subsets
def countP(n, k):
# Table to store results of subproblems
dp = [[0 for _ in range(k + 1)] for _ in range(n + 1)]
# Initialize base cases
for i in range(n + 1):
# No way to partition n elements
# into 0 subsets if n > 0
dp[i][0] = 0
# 1 way to partition 0 elements into 0 subsets
dp[0][0] = 1
# Fill the rest of the entries
# in dp[][] in bottom-up manner
for i in range(1, n + 1):
for j in range(1, k + 1):
if j == 1 or i == j:
# One way to partition i elements
# into i subsets or 1 subset
dp[i][j] = 1
else:
dp[i][j] = j * dp[i - 1][j] + \
dp[i - 1][j - 1]
return dp[n][k]
if __name__ == "__main__":
n = 5
k = 2
print(countP(n, k))
C#
// A Dynamic Programming based C# program to count
// number of partitions of a set with n elements
using System;
class GfG {
// Returns count of different partitions
// of n elements in k subsets
static int CountP(int n, int k) {
// Table to store results of
// subproblems
int[,] dp = new int[n + 1, k + 1];
// Initialize base cases
for (int i = 0; i <= n; i++) {
// No way to partition n elements
// into 0 subsets if n > 0
dp[i, 0] = 0;
}
for (int i = 0; i <= k; i++) {
// 1 way to partition 0 elements
// into 0 subsets
dp[0, i] = (i == 0) ? 1 : 0;
}
// Fill rest of the entries in
// dp[][] in bottom-up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
if (j == 1 || i == j) {
// One way to partition i elements
// into i subsets or 1 subset
dp[i, j] = 1;
} else {
dp[i, j] = j * dp[i - 1, j] + dp[i - 1, j - 1];
}
}
}
return dp[n, k];
}
static void Main() {
int n = 5, k = 2;
Console.WriteLine(CountP(n, k));
}
}
JavaScript
// A Dynamic Programming based JavaScript program to count
// number of partitions of a set with n elements
// Returns count of different partitions
// of n elements in k subsets
function countP(n, k) {
// Table to store results of subproblems
let dp = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
// Initialize base cases
for (let i = 0; i <= n; i++) {
// No way to partition n elements
// into 0 subsets if n > 0
dp[i][0] = 0;
}
for (let i = 0; i <= k; i++) {
// 1 way to partition 0 elements
// into 0 subsets
dp[0][i] = (i === 0) ? 1 : 0;
}
// Fill rest of the entries in
// dp[][] in bottom-up manner
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= i; j++) {
if (j === 1 || i === j) {
dp[i][j] = 1;
} else {
dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
}
}
}
return dp[n][k];
}
const n = 5, k = 2;
console.log(countP(n, k));
Using Space Optimized DP - O(n * k) Time and O(n) Space
In previous approach the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
- Create a 1D vector dp of size n+1.
- Set a base case dp[0] = 1 .
- Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
- Initialize variable prev and temp used to store the previous values from current computations.
- After every iteration assign the value of temp to prev for further iteration.
- At last return and print the final answer stored in dp[n].
C++
// C++ program to count different
// partitions of n elements into k subsets
#include<bits/stdc++.h>
using namespace std;
// Returns count of different partitions of n
// elements in k subsets
int countP(int n, int k) {
// Vector to store results of subproblems
vector<int> dp(n+1, 0);
// Base cases
dp[0] = 1;
for (int j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
int prev = dp[0];
for (int i = 1; i <= n; i++) {
// Store current value of dp[i]
int temp = dp[i];
if (j == 1 || i == j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
int main() {
cout << countP(5, 2);
return 0;
}
Java
// Java program to count different
// partitions of n elements into k subsets
class GfG {
// Returns count of different
// partitions of n elements in k subsets
static int countP(int n, int k) {
// Vector to store results of subproblems
int[] dp = new int[n+1];
// Base cases
dp[0] = 1;
for (int j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
int prev = dp[0];
for (int i = 1; i <= n; i++) {
// Store current value of dp[i]
int temp = dp[i];
if (j == 1 || i == j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
public static void main(String[] args) {
System.out.println(countP(5, 2));
}
}
Python
# Python program to count different
# partitions of n elements into k subsets
# Returns count of different partitions
# of n elements in k subsets
def countP(n, k):
# List to store results of subproblems
dp = [0] * (n + 1)
# Base cases
dp[0] = 1
for j in range(1, k + 1):
# Store previous value of dp[i-1]
prev = dp[0]
for i in range(1, n + 1):
# Store current value of dp[i]
temp = dp[i]
if j == 1 or i == j:
# One way to partition if j == 1 or i == j
dp[i] = 1
else:
dp[i] = j * dp[i - 1] + prev
# Update prev for next iteration
prev = temp
# Return the final answer
# (number of ways to partition n elements)
return dp[n]
if __name__ == "__main__":
print(countP(5, 2))
C#
// C# program to count different
// partitions of n elements into k subsets
class GfG {
// Returns count of different
// partitions of n elements in k subsets
static int countP(int n, int k) {
// Array to store results of subproblems
int[] dp = new int[n + 1];
// Base cases
dp[0] = 1;
for (int j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
int prev = dp[0];
for (int i = 1; i <= n; i++) {
// Store current value of dp[i]
int temp = dp[i];
if (j == 1 || i == j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
static void Main(string[] args) {
System.Console.WriteLine(countP(5, 2));
}
}
JavaScript
// JavaScript program to count different
// partitions of n elements into k subsets
// Returns count of different
// partitions of n elements in k subsets
function countP(n, k) {
// Array to store results of subproblems
let dp = new Array(n + 1).fill(0);
// Base cases
dp[0] = 1;
for (let j = 1; j <= k; j++) {
// Store previous value of dp[i-1]
let prev = dp[0];
for (let i = 1; i <= n; i++) {
// Store current value of dp[i]
let temp = dp[i];
if (j === 1 || i === j) {
// One way to partition if j == 1 or i == j
dp[i] = 1;
} else {
dp[i] = j * dp[i - 1] + prev;
}
// Update prev for next iteration
prev = temp;
}
}
// Return the final answer
// (number of ways to partition n elements)
return dp[n];
}
console.log(countP(5, 2));
Related article:
Similar Reads
Partition of a set into K subsets with equal sum Given an integer array arr[] and an integer k, the task is to check if it is possible to divide the given array into k non-empty subsets of equal sum such that every array element is part of a single subset.Examples: Input: arr[] = [2, 1, 4, 5, 6], k = 3 Output: trueExplanation: Possible subsets of
9 min read
Bell Numbers (Number of ways to Partition a Set) Given a set of n elements, find the number of ways of partitioning it. Examples: Input: n = 2Output: 2Explanation: Let the set be {1, 2}. The partitions are {{1},{2}} and {{1, 2}}.Input: n = 3Output: 5Explanation: Let the set be {1, 2, 3}. The partitions are {{1},{2},{3}}, {{1},{2, 3}}, {{2},{1, 3}}
15+ min read
Count numbers present in partitions of N Given an integer N, the task is to count the numbers in ordered integer partitions of N.Examples: Input: N = 3 Output: 8 Integer partitions of N(=3) are {{1 + 1 + 1}, {1 + 2}, {2 + 1}, {3}}. Numbers in integer partition of N are:{1, 1, 1, 1, 2, 2, 1, 3} Therefore, the count of numbers in integer par
3 min read
Print all possible ways to split an array into K subsets Given an array arr[] of size N and an integer K, the task is to print all possible ways to split the given array into K subsets. Examples: Input: arr[] = { 1, 2, 3 }, K = 2Output: { {{ 1, 2 }, { 3 }}, {{ 1, 3 }, { 2 }}, {{ 1 }, { 2, 3 }}}. Input: arr[] = { 1, 2, 3, 4 }, K = 2Output: { {{ 1, 2, 3 },
13 min read
Number of distinct subsets of a set Given an array of n distinct elements, count total number of subsets. Examples: Input : {1, 2, 3} Output : 8 Explanation: the array contain total 3 element.its subset are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {3, 1}, {1, 2, 3}. so the output is 8.. We know number of subsets of set of size n is 2n How d
3 min read