

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Perform Partition of an Integer in All Possible Ways
Here is a C++ Program to get all the unique partitions of a given integer such that addition of a partition results an integer. In this program, a positive integer n is given, and generate all possible unique ways to represent n as sum of positive integers.
Algorithm
Begin function displayAllUniqueParts(int m): Declare an array to store a partition p[m]. Set Index of last element k in a partition to 0 Initialize first partition as number itself, p[k]=m Create a while loop which first prints current partition, then generates next partition. The loop stops when the current partition has all 1s. Display current partition as displayArray(p, k + 1); Generate next partition: Initialize val=0. Find the rightmost non-one value in p[]. Also, update the val so that we know how much value can be accommodated. If k < 0, all the values are 1 so there are no more partitions Decrease the p[k] found above and adjust the val. If val is more, then the sorted order is violeted. Divide val in different values of size p[k] and copy these values at different positions after p[k]. Copy val to next position and increment position. End
Example Code
#include<iostream> using namespace std; void printArr(int p[], int m) { for (int i = 0; i < m; i++) cout << p[i] << " "; cout << endl; } void printAllUniqueParts(int m) { int p[m]; int k = 0; p[k] = m; while (true) { printArr(p, k + 1); int rem_val = 0; while (k >= 0 && p[k] == 1) { rem_val += p[k]; k--; } if (k < 0) return; p[k]--; rem_val++; while (rem_val > p[k]) { p[k + 1] = p[k]; rem_val = rem_val - p[k]; k++; } p[k + 1] = rem_val; k++; } } int main() { cout << "All Unique Partitions of 3\n"; printAllUniqueParts(3); cout << "\nAll Unique Partitions of 4\n"; printAllUniqueParts(4); cout << "\nAll Unique Partitions of 5\n"; printAllUniqueParts(5); return 0; }
Output
All Unique Partitions of 3 3 2 1 1 1 1 All Unique Partitions of 4 4 3 1 2 2 2 1 1 1 1 1 1 All Unique Partitions of 5 5 4 1 3 2 3 1 1 2 2 1 2 1 1 1 1 1 1 1 1
- Related Questions & Answers
- Finding all possible ways of integer partitioning in JavaScript
- C++ Program to Perform integer Partition for a Specific Case
- Golang Program to Generate all the Divisors of an Integer
- Bell Numbers - Number of ways to Partition a Set in C++
- C# program to find all duplicate elements in an integer array
- What is the maximum possible value of an integer in C# ?
- What is the maximum possible value of an integer in Python?
- What is the maximum possible value of an integer in Java ?
- Count number of ways to partition a set into k subsets in C++
- Count possible ways to construct buildings
- Finding all possible subsets of an array in JavaScript
- Print all possible ways to convert one string into another string in C++
- C++ program to Zoom digits of an integer
- C++ Program to Find number of Ways to Partition a word such that each word is a Palindrome
- Python program to find the sum of all even and odd digits of an integer list
Advertisements