-2

How to generate permutation with combination of a numbers.Can bit masking could be useful? For example:
If i have N digits i want to generate a number of M digits.How can it be done with minimum time complexity

user4084025
  • 17
  • 1
  • 7

1 Answers1

0

First, you can write an algorithm to generate all full permutations of a set (you can write a recursive function for that, see Algorithm to generate all possible permutations of a list?).

You can then write an algorithm to generate all sets of n digits out of m. You can do this recursively as well, metacode below.

generate(n, setOfM) {
    startingWithFirst = setOfM[0] concatenated with generate(n -1, setOfM[1..])
    notStartingWithFirst = generate(n, setOfM[1..])
    return startingWithFirst.Union(notStartingWithFirst))
}
Community
  • 1
  • 1
Kenny Hung
  • 442
  • 3
  • 10