Ad
import java.util.Arrays;

public class MaxNumber {
    public static long print(long number) {
        return number
    }
}
import java.util.Arrays;

public class MaxNumber {
    public static long print(long number) {
        return number
    }
}
import java.util.Arrays;

public class MaxNumber {
    public static long print(long number) {
        return number
    }
}
import java.util.Arrays;

public class MaxNumber {
    public static long print(long number) {
        return number
    }
}
Code
Diff
  • import java.util.Arrays;
    
    public class MaxNumber {
        public static long print(long number) {
            return number;
        }
        
    }
    • import java.util.Arrays;
    • public class MaxNumber {
    • public static long print(long number) {
    • char[] digits = Long.toString(number).toCharArray();
    • Arrays.sort(digits);
    • long maxNumber = 0;
    • // Traverse the sorted array in reverse order
    • // to get the maximum number
    • for (int i = digits.length - 1; i >= 0; i--) {
    • int digit = Character.getNumericValue(digits[i]);
    • maxNumber = maxNumber * 10 + digit;
    • }
    • return maxNumber;
    • return number;
    • }
    • }

Given a number, write a program to find a maximum number that can be formed using all of the digits of this number.

import java.util.Arrays;

public class MaxNumber {
    public static long print(long number) {
        char[] digits = Long.toString(number).toCharArray();
        Arrays.sort(digits);
        long maxNumber = 0;

        // Traverse the sorted array in reverse order
        // to get the maximum number
        for (int i = digits.length - 1; i >= 0; i--) {
            int digit = Character.getNumericValue(digits[i]);
            maxNumber = maxNumber * 10 + digit;
        }

        return maxNumber;
    }
    
}