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:

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

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter