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 Check whether Graph is a Bipartite using BFS
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Implement Pagoda
Spring Cloud – Tracing Services with Zipkin
Spring Boot - Thymeleaf
Java Program to implement Priority Queue
Java Program to Implement Interpolation Search Algorithm
Hướng dẫn sử dụng luồng vào ra nhị phân trong Java
Spring Security and OpenID Connect
Giới thiệu thư viện Apache Commons Chain
Java Program to Construct a Random Graph by the Method of Random Edge Selection
Java Program to Describe the Representation of Graph using Incidence Matrix
Configure a RestTemplate with RestTemplateBuilder
Immutable ArrayList in Java
Java Program to Implement the Hungarian Algorithm for Bipartite Matching
Intro to Inversion of Control and Dependency Injection with Spring
Exploring the Spring Boot TestRestTemplate
Exception Handling in Java
Java Program to Emulate N Dice Roller
Java Program to Implement Sorted Vector
Spring MVC Setup with Kotlin
Compact Strings in Java 9
Sử dụng CyclicBarrier trong Java
Giới thiệu Design Patterns
Java Program to Implement Ternary Heap
Spring Data JPA @Modifying Annotation
Java Program to Represent Graph Using Incidence Matrix
Lớp HashMap trong Java
OAuth2.0 and Dynamic Client Registration
Java – Write a Reader to File
Rate Limiting in Spring Cloud Netflix Zuul
Spring’s RequestBody and ResponseBody Annotations