Print all n-digit strictly increasing numbers - GeeksforGeeks
Given number of digits n in a number, print all n-digit numbers whose digits are strictly increasing from left to right.
Read full article from Print all n-digit strictly increasing numbers - GeeksforGeeks
Given number of digits n in a number, print all n-digit numbers whose digits are strictly increasing from left to right.
The idea is to use recursion. We start from the leftmost position of a possible N-digit number and fill it from set of all digits greater than its previous digit. i.e. fill current position with digits (i to 9] where i is its previous digit. After filling current position, we recurse for next position with strictly increasing numbers.
void
findStrictlyIncreasingNum(
int
start, string out,
int
n)
{
// If number becomes N-digit, print it
if
(n == 0)
{
cout << out <<
" "
;
return
;
}
// start from (prev digit + 1) till 9
for
(
int
i = start; i <= 9; i++)
{
// append current digit to number
string str = out + to_string(i);
// recurse for next digit
findStrictlyIncreasingNum(i + 1, str, n - 1);
}
}
Read full article from Print all n-digit strictly increasing numbers - GeeksforGeeks