C++ Program to Perform Partition of an Integer in All Possible Ways
Posted by Superadmin on August 09 2022 07:33:44

C++ Program to Perform Partition of an Integer in All Possible Ways

 

This is a C++ Program to find the unique partitions of a given integer such that the addition of a partition result an integer. Given a positive integer n, generate all possible unique ways to represent n as a sum of positive integers.

 

Here is source code of the C++ Program to demonstrate the Generation of All Unique Partitions of an Integer. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /* 
  2.  * C++ Program to Generate All Unique Partitions of an Integer 
  3.  */
  4. #include<iostream>
  5. using namespace std;
  6. /* 
  7.  * print an array p[] of size 'n'
  8.  */  
  9. void printArray(int p[], int n)
  10. {
  11.     for (int i = 0; i < n; i++)
  12.        cout << p[i] << " ";
  13.     cout << endl;
  14. }
  15.  
  16. void printAllUniqueParts(int n)
  17. {
  18.     int p[n]; // An array to store a partition
  19.     int k = 0;  // Index of last element in a partition
  20.     p[k] = n;  // Initialize first partition as number itself
  21.  
  22.     // This loop first prints the current partition then generates the next partition. 
  23.     // The loop stops when the current partition has all 1s
  24.     while (true)
  25.     {
  26.         // print current partition
  27.         printArray(p, k + 1);
  28.  
  29.         // Find the rightmost non-one value in p[]. Also, update the rem_val
  30.         // So that we know how much value can be accommodated
  31.  
  32.         int rem_val = 0;
  33.         while (k >= 0 && p[k] == 1)
  34.         {
  35.             rem_val += p[k];
  36.             k--;
  37.         }
  38.  
  39.         // if k < 0, all the values are 1 so there are no more partitions
  40.  
  41.         if (k < 0)  
  42.             return;
  43.  
  44.         // Decrease the p[k] found above and adjust the rem_val
  45.         p[k]--;
  46.         rem_val++;
  47.  
  48.         // If rem_val is more, then the sorted order is violeted.  
  49.         // Divide rem_val in differnt values of size p[k] 
  50.         // Copy these values at different positions after p[k]
  51.         while (rem_val > p[k])
  52.         {
  53.             p[k+1] = p[k];
  54.             rem_val = rem_val - p[k];
  55.             k++;
  56.         }
  57.  
  58.         // Copy rem_val to next position and increment position
  59.         p[k+1] = rem_val;
  60.         k++;
  61.     }
  62. }
  63.  
  64. /* 
  65.  * Main
  66.  */
  67. int main()
  68. {
  69.     int value;
  70.     while(1)
  71.     {
  72.         cout<<"Enter an Integer(0 to exit): ";
  73.         cin>>value;
  74.         if (value == 0)
  75.            break;
  76.         cout << "All Unique Partitions of "<<value<<endl;
  77.         printAllUniqueParts(value);
  78.         cout<<endl;
  79.     }
  80.     return 0;
  81. }

 

$ g++ unique_partitions.cpp
$ a.out
 
Enter an Integer(0 to exit): 2
All Unique Partitions of 2
2
1 1
 
Enter an Integer(0 to exit): 3
All Unique Partitions of 3
3
2 1
1 1 1
 
Enter an Integer(0 to exit): 4
All Unique Partitions of 4
4
3 1
2 2
2 1 1
1 1 1 1
 
Enter an Integer(0 to exit): 5
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
 
Enter an Integer(0 to exit): 7
All Unique Partitions of 7
7
6 1
5 2
5 1 1
4 3
4 2 1
4 1 1 1
3 3 1
3 2 2
3 2 1 1
3 1 1 1 1
2 2 2 1
2 2 1 1 1
2 1 1 1 1 1
1 1 1 1 1 1 1
 
Enter an Integer(0 to exit): 0
 
------------------
(program exited with code: 1)
Press return to continue