Here I'm sorting the digits and then reverse it so I get the Max Number
Time complexity: O(n log n)
The sorting operation has a time complexity of O(n log n) where n is the number of digits in the input number
import java.util.Arrays; public class MaxNumber { public static long print(long number) { String numStr = String.valueOf(number); char[] digits = numStr.toCharArray(); Arrays.sort(digits); StringBuilder sortedStr = new StringBuilder(new String(digits)); return Long.parseLong(sortedStr.reverse().toString()); } }
- import java.util.Arrays;
- public class MaxNumber {
- public static long print(long number) {
return number- String numStr = String.valueOf(number);
- char[] digits = numStr.toCharArray();
- Arrays.sort(digits);
- StringBuilder sortedStr = new StringBuilder(new String(digits));
- return Long.parseLong(sortedStr.reverse().toString());
- }
- }