Java Program to Perform Partition of an Integer in All Possible Ways

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:

Giới thiệu Google Guice – Aspect Oriented Programming (AOP)
A Guide to Queries in Spring Data MongoDB
Assert an Exception is Thrown in JUnit 4 and 5
Intersection of Two Lists in Java
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Prevent Brute Force Authentication Attempts with Spring Security
Hướng dẫn Java Design Pattern – Object Pool
Lập trình đa luồng trong Java (Java Multi-threading)
Spring Cloud AWS – S3
Jackson Unmarshalling JSON with Unknown Properties
Spring Boot - Database Handling
Guide to @JsonFormat in Jackson
Java Program to Find Nearest Neighbor for Static Data Set
Java Program to Perform the Sorting Using Counting Sort
A Guide to System.exit()
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Find the Vertex Connectivity of a Graph
Java Program to Implement K Way Merge Algorithm
Giới thiệu Google Guice – Dependency injection (DI) framework
Request Method Not Supported (405) in Spring
Java Program to Check whether Directed Graph is Connected using BFS
Java Program to Implement Stein GCD Algorithm
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
Java Program to Perform Preorder Recursive Traversal of a Given Binary Tree
Một số nguyên tắc, định luật trong lập trình
Serverless Functions with Spring Cloud Function
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Guide to Spring Cloud Kubernetes
Java Program to Implement Weight Balanced Tree
Guava Collections Cookbook
JPA/Hibernate Persistence Context
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set