Java Program to Perform Integer Partition for a Specific Case

This is a java program to generate and print all the partitions of a number such that when those partition elements are added results in the number itself, plus the partition should be unique. We start with the number, number minus one is the next partition and so on, till all one’s are the last partition where we stop.

Here is the source code of the Java Program to Perform integer Partition for a Specific Case. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to perform integer partition such that every partition is unique
  2. import java.util.Scanner;
  3.  
  4. public class Integer_Partition 
  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];
  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("Partitioning of a given Integer such that every partition is unique");
  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.  
  52. }

Output:

$ javac Integer_Partition.java
$ java Integer_Partition
 
Partitioning of a given Integer such that every partition is unique
Enter the number:
6
6 
5 1 
4 2 
4 1 1 
3 3 
3 2 1 
3 1 1 1 
2 2 2 
2 2 1 1 
2 1 1 1 1 
1 1 1 1 1 1

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement

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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification 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
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.