Circular Permutation in Binary

Medium

Description

Given n and start, return a permutation of integers 0 to 2^n - 1 starting with start where adjacent integers differ by one bit.

Examples

Input:n = 2, start = 3
Output:[3,2,0,1]
Explanation:

Gray code from 3.

Input:n = 1, start = 1
Output:[1,0]
Explanation:

For 1-bit Gray code starting from 1: there are only two possible values (0,1). Starting from 1, the Gray code sequence is [1,0] where each consecutive pair differs by exactly one bit.

Input:n = 3, start = 0
Output:[0,1,3,2,6,7,5,4]
Explanation:

For 3-bit Gray code starting from 0: the standard Gray code sequence [0,1,3,2,6,7,5,4] where each number differs from the next by exactly one bit. Binary representation: 000→001→011→010→110→111→101→100.

Constraints

  • 1 ≤ n ≤ 16

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!