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.
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:
- Initialization: Start with an empty array
currentto keep track of the current sequence of digits. - Recursive Function:
- Define a recursive function
generateNumbers(start, current)wherestartis the minimum digit that can be added to the current sequence. - If
currenthas exactly N digits, print/store this sequence as a valid number. - For each digit from
startto 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.
- Base and Recursive Cases:
- Base Case: If the length of
currentequals N, store or print the number. - Recursive Case: Iterate over possible digits, updating
currentand recursively generating further digits.
Example Code
- Time Complexity: The number of ways to choose N digits from 10 is given by the combination formula , which translates to in the worst-case scenario.
- Space Complexity: It utilizes depth proportional to the number of digits, 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
- Algorithm to generate all possible permutations of a list?
- Algorithm to generate anagrams
- Algorithm to generate bit mask
- Algorithm to generate mountain ranges with upstrokes and down-strokes java
- Algorithm to generate random 2D polygon
- Algorithm to generate spanning set
- Algorithm to generate RGB graduated colors in PHP
- Algorithm to get all possible string combinations from array up to certain length

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 courseTrack 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.