Partition Number in Java8 May 2025 | 5 min read In this section, we will learn what is a partition number and also create Java programs to check if the given number is a partition number or not. The partition number program is frequently asked in Java coding interviews and academics. Partition NumberIn combinatorics and number theory, partitioning a number K (greater than 0) means writing the number K using the sum of positive integers. Partition Number is also known as integer partition. Note that the ordering of the summands does not matter in doing the partition. Let's understand it with the help of an example. ApproachWe display all the partitions in sorted order and the numbers within a partition are also printed in the sorted order. The concept is to achieve the next partition with the help of the values in the current partition. We store each partition in the array ptt[]. We initialize the array ptt[] as K, where K is an input number. In each iteration, first, we print ptt[], and then update the array ptt[] to keep the next partition. Hence, our problem is reduced to find the next partition from the current partition. Steps to Find Next Partition Number with the help of Current PositionThe current partition is present in the array ptt[], and its size is also known. We are required to update ptt[] to keep the next partition. Values in the ptt[] should be sorted in the non-increasing order. Step 1: Look for the rightmost non-one value in the array ptt[] and keep the count of 1's encountered before the non-one value in a variable r_val (It shows the sum of the values on the right side that needs to be updated). Suppose, the index of the non-one value be i. Step 2: Reduce the value of ptt[i] by one and increase r_val by 1. Now there can be the following two cases:
Step 3: Copy ptt[i] to the next position, increment i and reduce the count by ptt[i] while ptt[i] is less than r_val. Eventually, put r_val at ptt[i + 1] and p[0…i + 1] is the new partition. The step is like splitting r_val in the terms of ptt[i] (4 is splitted in 2's). Examples of Partition NumbersLet K = 4, then K can be written as, 4 = 4 First way 3 + 1 = 4 Second way (1 + 3 is not considered, as ordering of summands has no impact in the partition) 2 + 2 = 4 Third way 2 + 1 + 1 = 4 Fourth way (Similarly, 1 + 2 + 1, 1 + 1 + 2 are not considered) 1 + 1 + 1 + 1 = 4 Fifth way Thus, there are five unique ways to split the number 4. The following figure shows the partitioning of numbers from 1 to 6. ![]() Partition Number Java ProgramLet's observe the Java program. FileName: PartitionNumberExample.java Output: All the Unique Partitions of 2 are: 2 1 1 All the Unique Partitions of 3 are: 3 2 1 1 1 1 All the Unique Partitions of 4 are: 4 3 1 2 2 2 1 1 1 1 1 1 All the Unique Partitions of 5 are: 5 4 1 3 2 3 1 1 2 2 1 2 1 1 1 1 1 1 1 1 Explanation: The Java program is based on the approach discussed above. The key here is to maintain the sorting in non-increasing order while doing the partition, and when there is violation follow the step 2 and step 3 defined above before the beginning of the program. Next TopicMultiply-two-strings-in-java |
Java Subtract Days from Current Date
In Java, dealing with date and time is not much difficult task because Java provides API for date and time that makes tasks easier for the developers. In this section, we will discuss how to subtract number of days from current date and any particular day. Using Java...
3 min read
Java Program to Check Whether an Array is a Permutation
Array Permutation To determine if array A is a permutation, ensure that it includes every number from 1 to n only once and does not have any repeated elements. It confirms that the array is a complete and valid sequence. Example of Array Permutation Example 1: Input: arr[] = {4,...
11 min read
Java Fibers
In this article, we are going to find out what are and when and where they are used in the Java programming language. What is ? The are also known as Java Virtual Machine (JVM) Fibers in the programming context. The JVM Fibers are user-mode threads...
3 min read
Online Java Compiler
An online compiler is a cloud-based IDE that helps the developer to compile and execute a Java program online without installing the JDK in the local system. In this section, we will discuss some popular online Java compilers that are available free of cost. The popular online...
6 min read
AbstractCollection addAll() Method in Java with Examples
The addAll() method under the Collections Framework is essential for mass addition of elements from one collection to another and this method is implemented in the AbstractCollection class which is under java. It belongs to the util package, and acts as skeletal implementation of the...
9 min read
Difference Between Static and Non-Static Members in Java
Java, a widely used object-oriented programming language, offers a variety of features to help build robust and flexible applications. Two important concepts in Java of the object model have static and non-static members. Understanding the difference between static and non-static members is important for effective Java...
5 min read
Sort Elements by Frequency in Java
An array of integers is given. Some elements of the array are repeated. Our task is to return an array or list of the provided elements in the decreasing order of their frequency of occurrence. In other words, the element which has the highest frequency of...
9 min read
Primitive Data Type Vs. Object Data Type in Java
Java Primitive Data Types Primitive data types in Java are the building blocks of data manipulation. They are the most basic data types available in the Java language. Java is a statically-typed programming language, which means that all variables must be declared before they are used. Primitive data...
5 min read
Program to Accept String Starting With Capital Letter in Java
A string is given to us as an input. The task is to determine whether the given string starts with the capital letter or not. Example 1: Input: String s = "Hello World" Output: It is a Valid String. Explanation: The given string starts with 'H', which is an uppercase letter. Example 2: Input: String s...
3 min read
DoubleBuffer asReadOnlyBuffer() method in Java with Examples
The java.nio.DoubleBuffer has an asReadOnlyBuffer() function. With the contents of this buffer, a new, read-only double buffer is created using the DoubleBuffer Class. The buffer is a duplicate of the new buffer. Therefore, any modifications made to the contents of this buffer will be included in...
4 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
