![]() |
VOOZH | about |
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.
Table of Content
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.
n-digit numbers 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
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.
1 to 9 as the first digit n, store the number 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