Java Program to Perform Partition of an Integer in All Possible Ways

This is a java program to perform partition of an integer in all possible ways. Every partition when added should result in the given integer.

Here is the source code of the Java Program to Perform Partition of an Integer in All Possible Ways. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is sample program to print a unique partitions of a given number
  2. import java.util.Scanner;
  3.  
  4. public class Unique_Partitions_Number 
  5. {
  6.     public static void print(int[]p, int n)
  7.     {
  8.         for(int i=0; i<n; i++)
  9.             System.out.print(p[i]+" ");
  10.         System.out.println();
  11.     }
  12.     public static void generateUniquePartition(int n)
  13.     {
  14.         int []p = new int[n+n];
  15.         int k = 0;
  16.         p[k] = n;
  17.         while(true)
  18.         {
  19.             print(p, k=1);
  20.             int rem_value = 0;
  21.             while(k >= 0 && p[k] == 1)
  22.             {
  23.                 rem_value += p[k];
  24.                 k--;
  25.             }
  26.             if(k < 0)
  27.                 return;
  28.  
  29.             p[k]--;
  30.             rem_value++;
  31.  
  32.             while(rem_value > p[k])
  33.             {
  34.                 p[k+1] = p[k];
  35.                 rem_value -= p[k];
  36.                 k++;
  37.             }
  38.             p[k+1] = rem_value;
  39.             k++;
  40.         }
  41.     }
  42.     public static void main(String args[])
  43.     {
  44.         System.out.println("Unique Partitioning of a given number");
  45.         System.out.println("Enter the number:");
  46.         Scanner sc = new Scanner(System.in);
  47.         int n = sc.nextInt();
  48.         generateUniquePartition(n);
  49.         sc.close();
  50.     }
  51. }

Output:

$ javac Unique_Partitions_Number.java
$ java Unique_Partitions_Number
Unique Partitioning of a given number
Enter the number:
4
 
4 
3 1
2 2
2 1 1
1 1 1 1

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

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

advertisement
advertisement
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.