Given 2 integers n
and start
. Your task is return any permutation p
of (0,1,2.....,2^n -1)
such that :
p[0] = start
p[i]
and p[i+1]
differ by only one bit in their binary representation.p[0]
and p[2^n -1]
must also differ by only one bit in their binary representation.
Example 1:
Input: n = 2, start = 3 Output: [3,2,0,1] Explanation: The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]
Example 2:
Input: n = 3, start = 2 Output: [2,6,7,5,4,0,1,3] Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).
Constraints:
1 <= n <= 16
0 <= start < 2 ^ n
The problem asks to find a circular permutation of integers from 0 to 2n - 1 such that consecutive numbers differ by only one bit in their binary representation, and the last and first numbers also differ by one bit. The permutation must start with a given start
integer.
The core idea revolves around Gray codes. A Gray code is a binary numeral system where two successive values differ in only one bit. We can leverage the property of Gray codes to construct the required permutation.
There are two main approaches:
Approach 1: Generate Gray Codes, then rearrange
n
bits. A simple way is using the formula gray(i) = i ^ (i >> 1)
.start
within the generated Gray codes.start
index, and concatenate the two parts to form the circular permutation.Approach 2: Optimized Gray Code Generation
This approach directly generates the desired permutation without the need for rearrangement. Since gray(0) = 0
, we can calculate gray(i) ^ start
for each i
from 0 to 2n - 1. This directly generates the permutation starting with start
.
Approach 1 (Generate, then rearrange):
Python:
def circularPermutation(n: int, start: int) -> List[int]:
gray_codes = [i ^ (i >> 1) for i in range(1 << n)] # Generate Gray codes
start_index = gray_codes.index(start) #Find index of start
return gray_codes[start_index:] + gray_codes[:start_index] #Rearrange
Java:
import java.util.*;
class Solution {
public List<Integer> circularPermutation(int n, int start) {
List<Integer> grayCodes = new ArrayList<>();
for (int i = 0; i < (1 << n); ++i) {
grayCodes.add(i ^ (i >> 1));
}
int startIndex = grayCodes.indexOf(start);
List<Integer> result = new ArrayList<>(grayCodes.subList(startIndex, grayCodes.size()));
result.addAll(grayCodes.subList(0, startIndex));
return result;
}
}
Approach 2 (Optimized Generation):
Python:
def circularPermutation(n: int, start: int) -> List[int]:
return [i ^ (i >> 1) ^ start for i in range(1 << n)]
Java:
import java.util.*;
class Solution {
public List<Integer> circularPermutation(int n, int start) {
List<Integer> result = new ArrayList<>();
for (int i = 0; i < (1 << n); ++i) {
result.add(i ^ (i >> 1) ^ start);
}
return result;
}
}
Approach 1:
Approach 2:
Approach 2 is slightly more efficient as it avoids the extra step of finding the index and rearranging the array. Both approaches have the same dominant time complexity.