C++ Program to Generate All the Set Partitions of n Numbers Beginning from 1 and so on

This is a C++ program to generate all the set partitions of n numbers beginning from 1 and so on.

Problem Description

1. This algorithm generates all the possible partition can be generated by breaking the given integer value.
2. The time complexity of this algorithm is O(n*n!).

Problem Solution

1. This algorithm takes the input of a natural number and prints all the possible partition of each number less than or equal to it.
2. For each number, it starts with the number and breaks it by 1, at a time.
3. Exit.

Program/Source Code

C++ Program to generate all the set partitions of n numbers beginning from 1 and so on.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

using namespace std;
// A function to print the generated partition.
void printArray(int p[], int n)
	for (int i = 0; i < n; i++)
		cout<<p[i]<<" ";
// A function to print all the possible partition.
void PrintAllUniqueParts(int n)
    int p[n], k = 0;
    p[k] = n;
    // Loop until all the array elements converted to 1 mean no further partition can be generated.
        printArray(p, k + 1);
        int rem_val = 0;
        // Move the pointer to the index so that p[k] > 1.
        while (k >= 0 && p[k] == 1)
            rem_val += p[k];
        // If k < 0 then the all the element are broken down to 1.
        if (k < 0)  
        // If value greater than 1 is found then decrease it by 1 and increase rem_val to add it to other elements.
        // Loop until the number of 1's are greater than the value at k index.
        while (rem_val > p[k])
            p[k+1] = p[k];
            // Decrease the rem_val value.
            rem_val = rem_val - p[k];
        // Assign the remaining value to the index next to k.
        p[k+1] = rem_val;
int main()
    int n;
    cout<<"Enter natural numbers for creating partitions: ";
	if (value <= 0)
		cout<<"Wrong input!!";
	cout<<"All Unique Partitions of "<<n<<" are:-\n";
    return 0;
Program Explanation

1. Take the input of the natural number ‘n’.
2. For a loop on ‘i’ from 1 to n, create a partition of each number.
3. Call PrintAllUniqueParts() with ‘i’ in the argument list.
4. Inside PrintAllUniqueParts(), declare an array p[] of length ‘n’ since we can generate maximum n partition, an element k to traverse in the array p[] and rem_val.
5. Assign n to the first element of array p[] and k to 0.
6. Now inside an infinite loop, print the array p[] and assign rem_val to 0, initially.
7. At the end of each iteration k Shifts to the end of the array to move it back to the first value encountered as greater than 0 and meanwhile add up all 1’s to rem_val.
8. If k reaches to -1 then breaks the loop and return to main.
9. To break value at k, decrement it and add it to rem_val.
10. So in the next loop, break rem_val till it is more than the value at index k, it will ignore the repetition.
11. Return to main.
12. Exit.

Runtime Test Cases
Case 1:
Enter natural numbers for creating partitions: 3
All Unique Partitions of 1 are:-
All Unique Partitions of 2 are:-
        1 1
All Unique Partitions of 3 are:-
        2 1
        1 1 1
Case 2:
Enter natural numbers for creating partitions: 5
All Unique Partitions of 1 are:-
All Unique Partitions of 2 are:-
        1 1
All Unique Partitions of 3 are:-
        2 1
        1 1 1
All Unique Partitions of 4 are:-
        3 1
        2 2
        2 1 1
        1 1 1 1
All Unique Partitions of 5 are:-
        4 1
        3 2
        3 1 1
        2 2 1
        2 1 1 1
        1 1 1 1 1

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.

If you find any mistake above, kindly email to [email protected]

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.