Open In App

Count number of ways to partition a set into k subsets

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 2 subsets in 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}}

Recursive Solution






// 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;
 
  // S(n+1, k) = k*S(n, k) + S(n, k-1)
  return  k*countP(n-1, k) + countP(n-1, k-1);
}
 
// Driver program
int main()
{
   cout <<  countP(3, 2);
   return 0;
}




// Java  program to count number
// of partitions of a set with
// n elements into k subsets
import java.io.*;
 
class GFG
{
    // Returns count of different
    // partitions of n elements in
    // k subsets
    public 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;
 
       // S(n+1, k) = k*S(n, k) + S(n, k-1)
       return (k * countP(n - 1, k)
              + countP(n - 1, k - 1));
    }
 
    // Driver program
    public static void main(String args[])
    {
       System.out.println(countP(3, 2));
 
    }
}
 
//This code is contributed by Anshika Goyal.




# A Python3 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
     
    # S(n+1, k) = k*S(n, k) + S(n, k-1)
    return (k * countP(n - 1, k) +
                countP(n - 1, k - 1))
 
# Driver Code
if __name__ == "__main__":
    print(countP(3, 2))
 
# This code is contributed
# by Akanksha Rai(Abby_akku)




// C# program to count number
// of partitions of a set with
// n elements into k subsets
using System;
 
class GFG {
     
    // Returns count of different
    // partitions of n elements in
    // k subsets
    public 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;
     
        // S(n+1, k) = k*S(n, k) + S(n, k-1)
        return (k * countP(n - 1, k)
                + countP(n - 1, k - 1));
    }
 
    // Driver program
    public static void Main()
    {
        Console.WriteLine(countP(3, 2));
    }
}
 
// This code is contributed by anuj_67.




<script>
// 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;
  
       // S(n + 1, k) = k*S(n, k) + S(n, k - 1)
       return (k * countP(n - 1, k)
              + countP(n - 1, k - 1));
    }
     
    // Driver program
    document.write(countP(3, 2));
     
    // This code is contributed by avanitrachhadiya2155   
</script>




<?php
// A PHP 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;
     
    // S(n+1, k) = k*S(n, k)
    // + S(n, k-1)
    return $k * countP($n - 1, $k) +
            countP($n - 1, $k - 1);
}
 
    // Driver Code
    echo countP(3, 2);
 
// This code is contributed by aj_36
?>

3

Efficient Solution  






// A Dynamic Programming based 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)
{
  // Table to store results of subproblems
  int dp[n+1][k+1];
 
  // Base cases
  for (int i = 0; i <= n; i++)
     dp[i][0] = 0;
  for (int i = 0; i <= k; i++)
     dp[0][k] = 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];
}
 
// Driver program
int main()
{
   cout <<  countP(5, 2);
   return 0;
}




// A Dynamic Programming based Java program to count
// number of partitions of a set with n elements
// into k subsets
import java.util.*;
import java.io.*;
 
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];
     
    // Base cases
    for (int i = 0; i <= n; i++)
    dp[i][0] = 0;
    for (int i = 0; i <= k; i++)
    dp[0][k] = 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)
        dp[i][j] = 1;
    else
        dp[i][j] = j * dp[i - 1][j] + dp[i - 1][j - 1];
         
        return dp[n][k];
     
}
 
// Driver program
public static void main(String[] args )
{
    System.out.println(countP(5, 2));
}
}
 
// This code is contributed by Rajput-Ji




# A Dynamic Programming based Python3 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):
     
    # Table to store results of subproblems
    dp = [[0 for i in range(k + 1)]
             for j in range(n + 1)]
 
    # Base cases
    for i in range(n + 1):
        dp[i][0] = 0
 
    for i in range(k + 1):
        dp[0][k] = 0
 
    # Fill 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):
                dp[i][j] = 1
            else:
                dp[i][j] = (j * dp[i - 1][j] +
                                dp[i - 1][j - 1])
                 
    return dp[n][k]
 
# Driver Code
if __name__ == '__main__':
    print(countP(5, 2))
 
# This code is contributed by
# Surendra_Gangwar




// A Dynamic Programming based C# program 
// to count number of partitions of a 
// set with n elements into k subsets
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];
     
    // Base cases
    for (int i = 0; i <= n; i++)
        dp[i, 0] = 0;
    for (int i = 0; i <= k; i++)
        dp[0, k] = 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)
                dp[i, j] = 1;
            else
                dp[i, j] = j * dp[i - 1, j] + dp[i - 1, j - 1];
             
        return dp[n, k];
     
}
 
// Driver code
public static void Main( )
{
    Console.Write(countP(5, 2));
}
}
 
// This code is contributed by Ita_c.




<script>
// A Dynamic Programming based 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)
    {
        // Table to store results of subproblems
        let dp = new Array(n+1);
        for(let i = 0; i < n + 1; i++)
        {
            dp[i] = new Array(k+1);
            for(let j = 0; j < k + 1; j++)
            {
                dp[i][j] = -1;
            }
        }
         
          
        // Base cases
        for (let i = 0; i <= n; i++)
            dp[i][0] = 0;
        for (let i = 0; i <= k; i++)
            dp[0][k] = 0;
          
        // Fill rest of the entries in dp[][]
        // in bottom up manner
        for (let i = 1; i <= n; i++)
            for (let j = 1; j <= k; 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];
    }
     
    // Driver program
    document.write(countP(5, 2))
     
    // This code is contributed by rag2127
</script>




<?php
// A Dynamic Programming based PHP
// 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)
{
     
// Table to store results
// of subproblems
$dp[$n + 1][$k + 1] = array(array());
 
// Base cases
for ($i = 0; $i <= $n; $i++)
    $dp[$i][0] = 0;
for ($i = 0; $i <= $k; $i++)
    $dp[0][$k] = 0;
 
// Fill rest of the entries in
// dp[][] in bottom up manner
for ($i = 1; $i <= $n; $i++)
    for ($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];
}
 
// Driver Code
echo countP(5, 2);
 
// This code is contributed by jit_t
?>

15

Efficient approach : Space optimization

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.

Implementation steps:

Implementation: 
 




//c++ code for above approach
#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++) {
        int prev = dp[0];
        for (int i = 1; i <= n; i++) {
            int temp = dp[i];
            if (j == 1 || i == j) {
                dp[i] = 1;
            } else {
                dp[i] = j * dp[i - 1] + prev;
            }
            prev = temp;
        }
    }
     
      // return finala answer
    return dp[n];
}
 
// Driver program
int main() {
    cout << countP(5, 2);
    return 0;
}




public class Main {
    // Function to count different partitions of n elements
    // in k subsets
    static int countPartitions(int n, int k)
    {
        // Create an array to store results of subproblems
        int[] dp = new int[n + 1];
 
        // Base cases
        dp[0] = 1;
 
        for (int j = 1; j <= k; j++) {
            int prev = dp[0];
            for (int i = 1; i <= n; i++) {
                int temp = dp[i];
                if (j == 1 || i == j) {
                    dp[i] = 1;
                }
                else {
                    dp[i] = j * dp[i - 1] + prev;
                }
                prev = temp;
            }
        }
 
        // Return the final answer
        return dp[n];
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int n = 5;
        int k = 2;
        System.out.println("Number of partitions: "
                           + countPartitions(n, k));
    }
}




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):
        prev = dp[0]
        for i in range(1, n + 1):
            temp = dp[i]
            if j == 1 or i == j:
                dp[i] = 1
            else:
                dp[i] = j * dp[i - 1] + prev
            prev = temp
 
    # Return the final answer
    return dp[n]
 
# Driver program
if __name__ == "__main__":
    print(countP(5, 2))




// c# code for above approach
using System;
 
class GFG
{
    // Returns count of different partitions of n
    // elements in k subsets
    static int CountP(int n, int k)
    {
        // store results of subproblems
        int[] dp = new int[n + 1];
        // Base cases
        dp[0] = 1;
        for (int j = 1; j <= k; j++)
        {
            int prev = dp[0];
            for (int i = 1; i <= n; i++)
            {
                int temp = dp[i];
                if (j == 1 || i == j)
                {
                    dp[i] = 1;
                }
                else
                {
                    // Otherwise, calculate the number of partitions using the recurrence relation:
                    // dp[i] = j * dp[i - 1] + prev
                    dp[i] = j * dp[i - 1] + prev;
                }
                prev = temp;
            }
        }
        // The final answer is stored in dp[n]
        return dp[n];
    }
    static void Main()
    {
        // Test the function with n=5 and k=2
        int n = 5;
        int k = 2;
        int result = CountP(n, k);
        Console.WriteLine(result);
    }
}




function GFG(n, k) {
    // Array to store results of
    // subproblems
    const dp = new Array(n + 1).fill(0);
    // Base cases
    dp[0] = 1;
    for (let j = 1; j <= k; j++) {
        let prev = dp[0];
        for (let i = 1; i <= n; i++) {
            const temp = dp[i];
            if (j === 1 || i === j) {
                dp[i] = 1;
            } else {
                dp[i] = j * dp[i - 1] + prev;
            }
            prev = temp;
        }
    }
    // Return the final answer
    return dp[n];
}
// Driver program
console.log(GFG(5, 2));

Output:

15

Time complexity: O(n*k). 
Space complexity: O(n). 
 

Similar Article: Bell Numbers 


Article Tags :