easyDigit ProblemsJava

Arrange the Digits of a Number in Descending Order

Reorder the digits of an integer so they appear from largest to smallest.

Quick Answer

Tally each digit into an int array of size 10 using number % 10. Then rebuild the result by walking the tally from index 9 down to 0, appending each digit as many times as it was counted with result = result * 10 + digit. This produces the digits arranged from largest to smallest without any sorting library.

Problem Statement

Given a non-negative integer, rearrange its digits so they appear in descending order (largest digit first) and return the resulting number. Do not convert the number to a String; use arithmetic only.

For example, 42315 becomes 54321. Repeated digits stay — 2020 becomes 2200.

Input: A single non-negative integer n.

Output: The integer whose digits are those of n sorted from largest to smallest.

Examples

Example 1
Input:  42315
Output: 54321

The digits 4, 2, 3, 1, 5 sorted from largest to smallest are 5, 4, 3, 2, 1.

Example 2
Input:  2020
Output: 2200

The digits are two 2s and two 0s; descending gives 2, 2, 0, 0.

Constraints

  • 0 <= n <= 2,147,483,647 (fits in a 32-bit int)
  • No String conversion; arithmetic only

Think Before You Code

Reveal the questions to ask yourself first
  • Digits only range 0-9 — can you sort them without a general sorting algorithm?
  • How do you count how many times each digit appears?
  • When rebuilding, in which order should you emit the digits for descending output?

Hints

Open them one at a time — try after each before revealing the next.

Hint 1
You do not need Arrays.sort — a tally of the ten possible digits is enough (counting sort).
Hint 2
Fill `count[n % 10]++` for every digit, then dropping n with `n / 10`.
Hint 3
Rebuild by looping the tally from index 9 down to 0; for each digit d, append it count[d] times with `result = result * 10 + d`.

Approach

Reveal the step-by-step approach

Because there are only ten possible digit values, a counting sort is the natural fit — no comparison sort required.

  1. Tally each digit: while n is not 0, do count[n % 10]++ and n = n / 10.
  2. Rebuild the answer by walking count from index 9 down to 0.
  3. For each index d, append it count[d] times using result = result * 10 + d.

Emitting from 9 downward guarantees the largest digits land in the highest place values, producing descending order.

Dry Run

Walk through the example step by step

Arranging n = 2020 in descending order:

tally phase: count[0]=2, count[2]=2

rebuild phase (index 9 -> 0):
index | count | result after appends
------+-------+----------------------
2     | 2     | 2, then 22
0     | 2     | 220, then 2200
end   |       | 2200

Solution

Reveal the full Java solution
public class DigitsDescending {
    public static long arrangeDescending(int n) {
        int[] count = new int[10];
        while (n != 0) {
            count[n % 10]++;
            n /= 10;
        }
        long result = 0;
        for (int d = 9; d >= 0; d--) {
            for (int k = 0; k < count[d]; k++) {
                result = result * 10 + d;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(arrangeDescending(42315)); // 54321
        System.out.println(arrangeDescending(2020));  // 2200
    }
}

Counting sort works here because the key space is tiny and fixed (digits 0-9), so we skip comparison sorting entirely. The return type is long so that rearranging the digits of a large int cannot overflow while we build the answer.

Time: O(d) to tally plus O(d) to rebuild, so O(d) overallSpace: O(1) — the tally is always 10 slots

Common Mistakes

  • Walking the tally from 0 to 9, which produces ascending order instead of descending.
  • Using an int accumulator and overflowing when the rearranged number exceeds the int range.

Edge Cases to Test

  • n = 0 returns 0 (the loop never runs).
  • Numbers containing zeros, like 2020, keep the zeros at the low end.
  • A number whose digits are already descending is returned unchanged.

Interview Follow-Ups

  • How would you arrange the digits in ascending order instead?
  • How would you handle leading zeros if ascending order pushes a zero to the front?

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Explore the Java Full Stack program →
Chat with us