VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-all-n-digit-strictly-increasing-numbers/

⇱ Print all n-digit strictly increasing numbers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all n-digit strictly increasing numbers

Last Updated : 26 May, 2026

Given an integer n, print all the n digit numbers in increasing order, such that their digits are in strictly increasing order(from left to right).

Examples:

Input: n = 1
Output: 0 1 2 3 4 5 6 7 8 9
Explanation: Single digit numbers are considered to be strictly increasing order.

Input: n = 2
Output: 12 13 14 15 16 17 18 19 23....79 89
Explanation: For n = 2, the correct sequence is 12 13 14 15 16 17 18 19 23 and so on up to 89.

[Naive Approach] Checking Every n-Digit Number – O(10ⁿ × n) Time and O(1) Space

The idea is to traverse every n-digit number and check whether its digits are strictly increasing from left to right. A number is valid if every digit is greater than the previous digit. Each number is converted into a string so that adjacent digits can be compared easily. All numbers satisfying this condition are stored in the result vector.

  • Determine the range of n-digit numbers
  • Traverse every number in this range
  • Convert the number into a string
  • Compare adjacent digits: If every next digit is greater than the previous digit, the number is valid
  • Store valid numbers in the result vector

Output
12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47 48 49 56 57 58 59 67 68 69 78 79 89 

[Expected Approach] Generating Valid Numbers Directly – O(C(10, n) × n) Time and O(n) Space

The idea is to generate only those numbers whose digits are strictly increasing instead of checking every possible number. We build numbers digit by digit. At every step, only digits greater than the previously chosen digit are allowed, ensuring the increasing property automatically. Once the constructed number reaches length n, it is added to the result.

  • Start recursion using digits 1 to 9 as the first digit
  • At every recursive call: Choose only digits greater than the last selected digit
  • Append the chosen digit to the current number
  • If the length becomes n, store the number

Output
12 13 14 15 16 17 18 19 23 24 25 26 27 28 29 34 35 36 37 38 39 45 46 47 48 49 56 57 58 59 67 68 69 78 79 89 


Comment
Article Tags:
Article Tags: