Write the shortest program that receives two signed integers
i and for each
i between 1 and
2^n - 1 returns the next ordered permutation based on the binary representation of the number. There is no specific order of the combinations but the number of 1s in the binary representation must always stay the same or grow and numbers each output may happen only once
The goal of this program is to generate all the combinations without repetitions of a set. To do so you may consider that each item is represented by a bit in a bit mask, that way if you have three items, A, B and C represented by 001, 010 and 100 respectively the combinations ABC, ACB, BCA, BAC, CAB and CBA are all represented as 111.
For increasing values of
i your program should output a new combinations always with the same number of elements or more.
You may read the input in the format
or just use
i variables, whichever suits you best.
You may output a single number
Each test case is two lines, input followed by output with a binary representation here for demonstration purposes:
3 1 1 (001) 3 2 4 (100) 3 3 2 (010) 3 4 6 (110) 3 7 7 (111)
- Shortest code wins (bytes)
- The result can be returned by a function or printed by your program.
- You may assume
ivariables already exist, there's no need to handle input if you don't want to.
- The answers with the same number of
1bits can be output in any order
- The input is guaranteed to be well formed and
1 <= i <= 2^n - 1.