Check Whether the Digits of a Number Are in Ascending Order
Determine whether an integer's digits strictly increase left to right, using arithmetic only.
Read the number's digits from the right with number % 10. For the digits to increase left to right, each digit you extract must be strictly less than the previous digit you extracted (the one to its right). Track that previous digit; if any extracted digit is greater than or equal to it, the digits are not ascending.
Problem Statement
Given a non-negative integer, decide whether its digits are in strictly
ascending order reading left to right. Return true if each digit is strictly
greater than the digit before it, and false otherwise. Do not convert the
number to a String; use arithmetic only.
For example, 1234 is ascending, while 1325 is not because 3 is followed
by the smaller digit 2.
Input: A single non-negative integer n.
Output: true if the digits of n strictly increase left to right, otherwise false.
Examples
Input: 1234
Output: trueEach digit is strictly greater than the one before it: 1 < 2 < 3 < 4.
Input: 1325
Output: falseReading left to right, 3 is followed by 2, which breaks the increasing order.
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
- Modulo gives you digits from the right — how does that relate to left-to-right order?
- If digits go up from left to right, how do they behave from right to left?
- Should equal adjacent digits count as ascending?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Modulo naturally peels digits off the right end, which is the reverse of the direction we care about. If the digits increase left to right, then scanning right to left they must strictly decrease.
- Start
prev = 10(larger than any digit, so the first comparison passes). - While
nis not 0:digit = n % 10.- If
digit >= prev, the strict increase is broken — returnfalse. - Set
prev = digit. n = n / 10.
- If the loop finishes, return
true.
Dry Run
Walk through the example step by step
Checking n = 1325:
step | n | digit | prev before | digit >= prev? | action
-----+------+-------+-------------+----------------+-----------
1 | 1325 | 5 | 10 | no | prev = 5
2 | 132 | 2 | 5 | no | prev = 2
3 | 13 | 3 | 2 | yes | return false
Solution
Reveal the full Java solution
public class AscendingDigits {
public static boolean digitsAscending(int n) {
int prev = 10; // sentinel larger than any single digit
while (n != 0) {
int digit = n % 10;
if (digit >= prev) return false;
prev = digit;
n /= 10;
}
return true;
}
public static void main(String[] args) {
System.out.println(digitsAscending(1234)); // true
System.out.println(digitsAscending(1325)); // false
}
}
The trick is realizing the % 10 scan runs opposite to the direction of the
question, so a left-to-right strictly increasing sequence becomes a right-to-left
strictly decreasing one. Starting prev at 10 lets the first digit always pass
the check without a special case.
O(d) where d is the number of digitsSpace: O(1)Common Mistakes
- Using > instead of >= for the comparison, which wrongly accepts equal adjacent digits as ascending.
- Comparing in the wrong direction and accepting descending numbers as ascending.
Edge Cases to Test
- A single-digit number is trivially ascending and returns true.
- Numbers with equal adjacent digits, like 1123, return false under strict ascending.
- n = 0 returns true (no digits break the order).
Interview Follow-Ups
- How would you allow non-decreasing order (equal adjacent digits allowed)?
- How would you check for strictly descending digits instead?
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 →