How to find all possible combinations?

A number is called 'desirable' if all the digits are strictly ascending eg: 159 as 1<5<9. You know that your rival has a strictly numeric password that is 'desirable'. Your close ally has given you the number of digits (N) in your rival's password. WAP that takes in 'N' as input and prints out all possible 'desirable' numbers that can be formed with N digits.


I have been thinking of the solution for over a day now but no luck. Just helping out with the pseudo-code will be great.

Highly Apprciated.
Just need some help to get started on it.
Humm something like this?

3 digits

1
2
3
4
5
for (int i = 1; i < 10; ++i)
  for (int j = i; j < 10; ++j)
    for (int k = j; k < 10; ++k)
      if ( i != j && j != k )
        cout << i << j << k << endl;


now do that more generic for n digits (you can solve it recursively as well)

Last edited on
Those would be the permutations; then you'd just have to check for the 'desirable' ones.

Also note that the STL has a next_permutation algorithm that will give you permutations. Its complexity is linear.
Topic archived. No new replies allowed.