Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
p[0] = startp[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 <= 160 <= start < 2 ^ nProblem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Backtracking · Bit Manipulation
2 3
3 2
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1238: Circular Permutation in Binary Representation
class Solution {
public List<Integer> circularPermutation(int n, int start) {
int[] g = new int[1 << n];
int j = 0;
for (int i = 0; i < 1 << n; ++i) {
g[i] = i ^ (i >> 1);
if (g[i] == start) {
j = i;
}
}
List<Integer> ans = new ArrayList<>();
for (int i = j; i < j + (1 << n); ++i) {
ans.add(g[i % (1 << n)]);
}
return ans;
}
}
// Accepted solution for LeetCode #1238: Circular Permutation in Binary Representation
func circularPermutation(n int, start int) []int {
g := make([]int, 1<<n)
j := 0
for i := range g {
g[i] = i ^ (i >> 1)
if g[i] == start {
j = i
}
}
return append(g[j:], g[:j]...)
}
# Accepted solution for LeetCode #1238: Circular Permutation in Binary Representation
class Solution:
def circularPermutation(self, n: int, start: int) -> List[int]:
g = [i ^ (i >> 1) for i in range(1 << n)]
j = g.index(start)
return g[j:] + g[:j]
// Accepted solution for LeetCode #1238: Circular Permutation in Binary Representation
struct Solution;
impl Solution {
fn circular_permutation(n: i32, start: i32) -> Vec<i32> {
let mut res = vec![];
for i in 0..1 << n {
res.push(start ^ (i ^ i >> 1));
}
res
}
}
#[test]
fn test() {
let n = 2;
let start = 3;
let res = vec![3, 2, 0, 1];
assert_eq!(Solution::circular_permutation(n, start), res);
let n = 3;
let start = 2;
let res = vec![2, 3, 1, 0, 4, 5, 7, 6];
assert_eq!(Solution::circular_permutation(n, start), res);
}
// Accepted solution for LeetCode #1238: Circular Permutation in Binary Representation
function circularPermutation(n: number, start: number): number[] {
const ans: number[] = [];
for (let i = 0; i < 1 << n; ++i) {
ans.push(i ^ (i >> 1) ^ start);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.