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.
//This is sample program to print a unique partitions of a given number
import java.util.Scanner;
public class Unique_Partitions_Number
{
public static void print(int[]p, int n)
{
for(int i=0; i<n; i++)
System.out.print(p[i]+" ");
System.out.println();
}
public static void generateUniquePartition(int n)
{
int []p = new int[n+n];
int k = 0;
p[k] = n;
while(true)
{
print(p, k=1);
int rem_value = 0;
while(k >= 0 && p[k] == 1)
{
rem_value += p[k];
k--;
}
if(k < 0)
return;
p[k]--;
rem_value++;
while(rem_value > p[k])
{
p[k+1] = p[k];
rem_value -= p[k];
k++;
}
p[k+1] = rem_value;
k++;
}
}
public static void main(String args[])
{
System.out.println("Unique Partitioning of a given number");
System.out.println("Enter the number:");
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
generateUniquePartition(n);
sc.close();
}
}
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
Related posts:
Java Program to Generate a Random Subset by Coin Flipping
LinkedHashSet trong Java hoạt động như thế nào?
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Spring Cloud – Adding Angular
Java Program to Find the Median of two Sorted Arrays using Binary Search Approach
Beans and Dependency Injection
Using JWT with Spring Security OAuth (legacy stack)
Java Program to Implement Sieve Of Sundaram
Java Program to Create a Minimal Set of All Edges Whose Addition will Convert it to a Strongly Conne...
Java Program to Implement Patricia Trie
Java Program to Check whether Graph is a Bipartite using BFS
Guide to Escaping Characters in Java RegExps
Java Program to Implement Find all Back Edges in a Graph
Dynamic Proxies in Java
Java Program to do a Depth First Search/Traversal on a graph non-recursively
Java Program to Implement Self organizing List
Comparing Strings in Java
Jackson Ignore Properties on Marshalling
A Guide to JPA with Spring
Spring Boot - Cloud Configuration Server
Spring AMQP in Reactive Applications
JUnit 5 @Test Annotation
Giới thiệu Google Guice – Binding
Java Program to Remove the Edges in a Given Cyclic Graph such that its Linear Extension can be Found
Giới thiệu về Stream API trong Java 8
Java Program to Implement HashMap API
Wrapper Classes in Java
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
A Quick Guide to Spring MVC Matrix Variables
Java Program to Use Boruvka’s Algorithm to Find the Minimum Spanning Tree
Count Occurrences of a Char in a String
Mapping a Dynamic JSON Object with Jackson