Next Greater Number with Same Set of Digits in Java10 Sept 2024 | 9 min read A number (num) is given. The task is to find a number that is the smallest that comprises the same set of digits as num and must be larger than the number num. If the number num is the largest number possible with its digits set, then it is not possible to find the next greater number, and hence, a message should be displayed accordingly. Example 1: Input: 89321 Output: 91238 Explanation: Numbers that are made of the digits 1, 2, 3, 8, 9 and are larger than the number 89123 are: 98123, 98132, 98213, 98231, 98312, 98321, 93128, 93182, 93218, 93281, 93812, 93821, 92138, 92183, 92318, 92381, 92813, 92831, 91238, 91283, 91328, 91382, 91823, 91832 In all these number the smallest number is 91238. Example 2: Input: 3648 Output: 3684 Explanation: Numbers that are made of the digits 3, 8, 4, 6 and are larger than 3648 are: 3684, 4368, 4386, 4638, 4683, 4836, 4863, 6348, 6384, 6438, 6483, 6834, 6843, 8346, 8364, 8436, 8463, 8634, 8643 In all these number the smallest number is 3684. Example 3: Input: 9100 Output: It is not possible to find the next greater number. Explanation: The largest number that is made of the digits 9, 1, 0, 0 is 9100. Thus, it is not possible to find a number that is greater than 9100 using the given digits. ObservationThe following are some of the important points of observation.
AlgorithmThe following is the algorithm for computing the next greater number on the basis of the observation described above. Step 1: Traverse the number num from the rightmost digit, keep traversing till one finds a digit that is lesser than the last traversed digit. For example, if one takes the input number as "4975", one has to stop at 4. It is because 4 is lesser than the next digit, 9. If one does not get such a digit, then it is not possible to find the next greater number. Step 2: Now start searching the right side of the above found digit 'd' for the smallest digit larger than 'd'. For the number 4975, the right side of 4 contains 975. Out of the digits 9, 7, and 5, the smallest digit larger than 4 is 5. Step 3: Now, swap the numbers 4 and 5. Thus, one will get 5974. However, this number is not the smallest among the largest. Therefore, we need to sort the digits after digit 5. Thus, by sorting the digits of the number 5974, one will get 5479. The number 5479 is the smallest among the numbers that are greater than the number 4975 and comprising of the digits 4, 9, 7, 5. Using PermutationIt is a brute force approach. In this approach, we will be computing each number that can be formed using the digits present in the given number num. We will be storing those computed numbers in a hash set hs. In that hash set, we will be picking a number that is minimum as well as greater than the number num. FileName: NextGreaterNumber.java Output: For the number: 536479 The next greater number is: 536497 For the number: 654321 The next greater number is not possible. For the number: 4876 The next greater number is: 6478 Complexity Analysis: In the above program, we have used backtracking to find the next greater number. Thus, the time complexity of the above program is exponential. Suppose the number num has d digits. Thus, the total numbers present in the hash set hs n!. Therefore, the space complexity of the above program is O(n!). Using SortingFileName: NextGreaterNumber Output: For the number: 536479 The next greater number is: 536497 For the number: 654321 The next greater number is not possible. For the number: 4876 The next greater number is: 6478 Complexity Analysis: In the above program, we have used loops as well as sorting. The loop takes O(n) time, and the sorting takes O(n * log(n)) time. Hence, the total time complexity of the program is O(n + n * log(n)), where n is the total number of digits present in the input number. As no extra space is used; therefore, the space complexity of the program is constant, i.e., O(1). Note: We can even reduce the time complexity further. Note that instead of sorting, we can also use the inbuild reverse method to do the sorting. It is because the digits that we are sorting are arranged in descending order. For example, sorting 6, 5, 4, 3, 2, 1 should not take O(n x log(n)) time. Only reversing the order will do the job. So, instead of sorting, use the reverse() method to reduce the time complexity to O(n).In the method isSwapPossible(), make the following changes to get the same result. |
The Java ConcurrentSkipListSet class implements the Collection interface and the AbstractSet class as a component of the Java Collection Framework. It offers the concurrent, scalable version of NavigableSet in Java. ConcurrentSkipListSet is based on ConcurrentSkipListMap in its implementation. Depending on whether constructor is used, the entries...
16 min read
Abstract Syntax Tree is a computer language's abstract syntactic structure is represented by a type of tree called a tree. A construct that is present in the source code is indicated by each node of the tree. Typically, an AST is the output of a compiler's syntax...
3 min read
Oracle Corporation will release Java Development Kit (JDK) 23 on September 17, 2024. It is a long-awaited release. It includes various new features, enhancements and updates. New enhancement increases the performance, security and the overall experience of developers. This release is currently in Initial Release Candidate. It...
14 min read
In this section, we will understand how to print a matrix in a diagonal order. Also, create a Java program that prints the matrix in a diagonal order. Diagonal Order First, we will understand the diagonal printing order. Consider the following matrix having 4 rows and 5 columns. The...
3 min read
The FloatBuffer put() has mainly 2 methods that take two different parameters. put(float f) put(int index, float f) i. put(float f) The java.nio.FloatBuffer class has put(float f) function. The newly generated float buffer is written with the specified float at the current location, and the position is then incremented...
5 min read
In object-oriented programming, abstraction is defined as hiding the unnecessary details (implementation) from the user and focusing on essential information (functionality). It increases the efficiency and reduces complexity. In Java, abstraction can be achieved using abstract classes and methods. Abstract Method In Java, an abstract method is...
5 min read
Bitwise Left Shift Operator (<<) Left shift operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<. When you write x<<n, the meaning is to shift the bits of x towards left n specified positions. Example If...
3 min read
Generic is used for creating Java code for graphs. Java's HashMap class is used to implement the Graph class. As we know, HashMap has a key and a value; in the graph, nodes are represented as keys, and their adjacency is listed as values. What is Generic? Generics...
9 min read
In Java, the Singleton pattern and static classes are employed to control instance creation and access to class-level behavior, but they serve distinct purposes and possess different characteristics. Singleton Pattern The Singleton pattern in Java is a design pattern that guarantees the existence of only one instance...
6 min read
In the world of programming, languages come and go, but a few remain as timeless classics. Java has undoubtedly been one of those classics, but it's time to take a look at its younger, more versatile sibling: Kotlin. Kotlin, introduced by JetBrains in 2011, has steadily...
10 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