Problem 56448. nth permutation of 11...100...0
Given some number of ones and zeros, numOnes and numZeros respectively, find the nth permutation of the vector [ones(1,numOnes), zeros(1,numZeros)] according to the lexicographical order. For example, if numOnes = 3 and numZeros = 2 then there are nchoosek(5,3) = 10 permutations of 11100. The lexicographic numbering is below
1 11100
2 11010
3 11001
4 10110
5 10101
6 10011
7 01110
8 01101
9 01011
10 00111
so for instance nthPerm(3,2,8) = [0,1,1,0,1]. You can assume that numOnes and numZeros will always be greater than or equal to 1. Lastly, your code should not enumerate all possibilities since one of the test cases contains billions of permutations.
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers3
Suggested Problems
-
Increment a number, given its digits
667 Solvers
-
"Low : High - Low : High - Turn around " -- Create a subindices vector
551 Solvers
-
148 Solvers
-
Who has power to do everything in this world?
459 Solvers
-
Create a two dimensional zero matrix
504 Solvers
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!