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:
How to Get a Name of a Method Being Executed?
Mapping a Dynamic JSON Object with Jackson
Spring Webflux and CORS
Assert an Exception is Thrown in JUnit 4 and 5
More Jackson Annotations
Java – Write to File
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Jackson – Unmarshall to Collection/Array
Java Program to Represent Graph Using Incidence Matrix
Generating Random Numbers in a Range in Java
The Dining Philosophers Problem in Java
Java Program to implement Circular Buffer
How to Define a Spring Boot Filter?
Spring MVC Async vs Spring WebFlux
Java Program to Implement Queue using Linked List
Spring Boot - Twilio
Intersection of Two Lists in Java
Java – Reader to String
Summing Numbers with Java Streams
Java Program to Implement LinkedBlockingDeque API
Spring Boot - Sending Email
Limiting Query Results with JPA and Spring Data JPA
A Quick Guide to Spring MVC Matrix Variables
Registration – Password Strength and Rules
Java Program to Implement Brent Cycle Algorithm
Netflix Archaius with Various Database Configurations
New Features in Java 9
Java Program to Find Transitive Closure of a Graph
Mapping Nested Values with Jackson
Java Program to Implement RoleList API
Java Program to Perform Search in a BST
Creating a Web Application with Spring 5