Count number of ways to partition a set into k subsets | A C ++ 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 ; Base cases ; S ( n + 1 , k ) = k * S ( n , k ) + S ( n , k - 1 ) ; Driver program
#include <iostream> NEW_LINE using namespace std ; int countP ( int n , int k ) { if ( n == 0 k == 0 k > n ) return 0 ; if ( k == 1 k == n ) return 1 ; return k * countP ( n - 1 , k ) + countP ( n - 1 , k - 1 ) ; } int main ( ) { cout << countP ( 3 , 2 ) ; return 0 ; }