algorithms
number generation
increasing order
combinatorics
mathematical computing

Algorithm to generate all possible N-digit numbers with whose digits are in increasing order

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Generating all possible N-digit numbers with digits in increasing order is a combinatorial problem that can be elegantly solved using recursive backtracking. These numbers are not merely permutations of digits but are constructed under the constraint that the sequence of digits must never decrease.

Problem Definition

Given a number N, generate all possible N-digit numbers where the digits are in strictly increasing order. Each digit in the number can range from 0 to 9 but must be strictly increasing as you move from the most significant to the least significant digit.

Approach

Understanding Constraints

The main constraint is that each subsequent digit must be greater than the previous. For example:

  • For N = 3, the valid integers could be [012, 013, 014, ..., ..., 789].

Combinatorial Nature

The problem aligns itself with the concept of combinations in combinatorics. Specifically, choosing N distinct digits from a set (0-9) and sorting these in increasing order is essentially forming a combination.

Recursive Backtracking Algorithm

One effective way to generate these increasing N-digit numbers is through recursive backtracking. This approach allows for exploring all potential combinations while adhering to the constraints.

Algorithm Steps:

  1. Initialization: Start with an empty array current to keep track of the current sequence of digits.
  2. Recursive Function:
    • Define a recursive function generateNumbers(start, current) where start is the minimum digit that can be added to the current sequence.
    • If current has exactly N digits, print/store this sequence as a valid number.
    • For each digit from start to 9 (inclusive):
      • Append the digit to current .
      • Recursively call the function with the next starting digit digit + 1 .
      • Backtrack by removing the last added digit.
  3. Base and Recursive Cases:
    • Base Case: If the length of current equals N, store or print the number.
    • Recursive Case: Iterate over possible digits, updating current and recursively generating further digits.

Example Code

  • Time Complexity: The number of ways to choose N digits from 10 is given by the combination formula C(10,N)C(10, N), which translates to O(10N)O(10^N) in the worst-case scenario.
  • Space Complexity: It utilizes depth proportional to the number of digits, O(N)O(N) for recursion depth.
  • Generating numbers of a specified maximum length.
  • Generating numbers within a given range.
  • Allowing some digits to be repeated, thus modifying the uniqueness constraint.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms