1238. Circular Permutation in Binary Representation Medium

@problem@discussion
#Math#Backtracking#Bit Manipulation



1/**
2 * [1238] Circular Permutation in Binary Representation
3 *
4 * Given 2 integers n and start. Your task is return any permutation p of (0,1,2.....,2^n -1) such that :
5 * 
6 * 
7 * 	p[0] = start
8 * 	p[i] and p[i+1] differ by only one bit in their binary representation.
9 * 	p[0] and p[2^n -1] must also differ by only one bit in their binary representation.
10 * 
11 * 
12 *  
13 * Example 1:
14 * 
15 * 
16 * Input: n = 2, start = 3
17 * Output: [3,2,0,1]
18 * Explanation: The binary representation of the permutation is (11,10,00,01). 
19 * All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]
20 * 
21 * 
22 * Example 2:
23 * 
24 * 
25 * Input: n = 3, start = 2
26 * Output: [2,6,7,5,4,0,1,3]
27 * Explanation: The binary representation of the permutation is (010,110,111,101,100,000,001,011).
28 * 
29 * 
30 *  
31 * Constraints:
32 * 
33 * 
34 * 	1 <= n <= 16
35 * 	0 <= start < 2 ^ n
36 * 
37 */
38pub struct Solution {}
39
40// problem: https://leetcode.com/problems/circular-permutation-in-binary-representation/
41// discuss: https://leetcode.com/problems/circular-permutation-in-binary-representation/discuss/?currentPage=1&orderBy=most_votes&query=
42
43// submission codes start here
44
45impl Solution {
46    pub fn circular_permutation(n: i32, start: i32) -> Vec<i32> {
47        
48    }
49}
50
51// submission codes end
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_1238() {
59    }
60}
61


Back
© 2025 bowen.ge All Rights Reserved.