Ad
Code
Diff
  • import java.util.Arrays;
    
    public class MaxNumber {
        public static long print(long number) {
            int[] digits = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
          
            for (char digit : Long.toString(number).toCharArray()) {
              digits[digit - '0']++;
            }
          
            long sum = 0;
            int i = 9;
          
            while(i >= 0) {
              if (digits[i] == 0) {
                i--;
              } else {
                digits[i]--;
                sum = (10 * sum) + i;
              }
            }
          
            return sum;
        }
    }
    • import java.util.Arrays;
    • public class MaxNumber {
    • public static long print(long number) {
    • long sum = 0;
    • String numStr = String.valueOf(number);
    • char[] digits = numStr.toCharArray();
    • Arrays.sort(digits);
    • int[] digits = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    • long[] powersOfTen = new long[20]; // precalculated powers of ten
    • for (int i = 19; i >= 0; --i) {
    • powersOfTen[i] = (long) Math.pow(10, i);
    • for (char digit : Long.toString(number).toCharArray()) {
    • digits[digit - '0']++;
    • }
    • // here the digits are sorted in ascending order
    • for (int i = 0; i < digits.length; i++) {
    • sum += Character.getNumericValue(digits[i]) * powersOfTen[i];
    • long sum = 0;
    • int i = 9;
    • while(i >= 0) {
    • if (digits[i] == 0) {
    • i--;
    • } else {
    • digits[i]--;
    • sum = (10 * sum) + i;
    • }
    • }
    • return sum;
    • }
    • }