Just smaller number with monotone digits in JavaScript

In JavaScript, finding the largest number with monotonically increasing digits that is less than or equal to a given number is a common algorithmic problem. A number has monotonically increasing digits when each digit is greater than or equal to the previous digit.

What are Monotonically Increasing Digits?

An integer has monotone increasing digits if and only if each pair of adjacent digits x and y satisfy x ? y. For example, 1234 and 1139 have monotone increasing digits, while 332 does not.

Problem Statement

We need to write a JavaScript function that takes a number as input and returns the largest number that is less than or equal to the input with monotone increasing digits.

For example:

  • Input: 332 ? Output: 299
  • Input: 1234 ? Output: 1234 (already monotonic)
  • Input: 54321 ? Output: 49999

Solution Approach

The algorithm works by:

  1. First checking if the number already has monotone increasing digits
  2. If not, finding the first position where the monotone property breaks
  3. Reducing the digit at that position by 1 and filling all subsequent positions with 9

Implementation

const monotoneIncreasingDigits = (num) => {
    // Helper function to check if a number has monotone increasing digits
    const checkMonotone = (x) => {
        if (x <= 9) {
            return true;
        }
        let currentDigit = x % 10;
        while (x) {
            const next = Math.floor(x / 10);
            const nextDigit = next % 10;
            if (currentDigit >= nextDigit) {
                currentDigit = nextDigit;
                x = next;
            } else {
                return false;
            }
        }
        return true;
    };

    // If already monotone, return the number itself
    if (checkMonotone(num)) {
        return num;
    }

    // Convert to digits array and find the solution
    const digits = num.toString().split('').map(x => Number(x));
    return digits.reduce((acc, digit, index) => {
        if (digit >= 1) {
            // Create candidate by reducing current digit and filling rest with 9s
            const prefix = digits.slice(0, index).join('');
            const reducedDigit = digit - 1;
            const suffix = new Array(digits.length - index - 1).fill('9').join('');
            const candidate = parseInt(prefix + reducedDigit + suffix, 10);
            
            if (checkMonotone(candidate)) {
                return Math.max(acc, candidate);
            }
        }
        return acc;
    }, 0);
};

// Test examples
console.log(monotoneIncreasingDigits(332));   // Should output 299
console.log(monotoneIncreasingDigits(1234));  // Should output 1234
console.log(monotoneIncreasingDigits(54321)); // Should output 49999
299
1234
49999

How It Works

The checkMonotone function verifies if a number has monotone increasing digits by comparing adjacent digits from right to left. The main function tries different candidates by reducing each digit and filling the remaining positions with 9s, then selects the maximum valid result.

Key Points

  • Single-digit numbers are always monotone increasing
  • The algorithm ensures we find the largest possible valid number
  • Time complexity is O(n²) where n is the number of digits
  • Works for any positive integer input

Conclusion

This solution efficiently finds the largest number with monotonically increasing digits by systematically reducing digits and maximizing the result. The approach handles edge cases and ensures optimal solutions for any input number.

Updated on: 2026-03-15T23:19:00+05:30

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements