The Indian Engineer

Problem 89 Gray Code

Posted on Last edited 3 mins

Math Bit-Manipulation

Problem Statement

Link - Problem 89

Question

An n-bit gray code sequence is a sequence of 2^n integers where:

Given an integer n, return any valid n-bit gray code sequence.

Example 1

Input: n = 2
Output: [0,1,3,2]
Explanation:
The binary representation of [0,1,3,2] is [00,01,11,10].
- 00 and 01 differ by one bit
- 01 and 11 differ by one bit
- 11 and 10 differ by one bit
- 10 and 00 differ by one bit
[0,2,3,1] is also a valid gray code sequence, whose binary representation is [00,10,11,01].
- 00 and 10 differ by one bit
- 10 and 11 differ by one bit
- 11 and 01 differ by one bit
- 01 and 00 differ by one bit

Example 2

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

Constraints

Solution

class Solution {
public:
    vector<int> grayCode(int n) {
        int size = 1<<n;
        vector<int>ans(size);

        for(int i = 0;i<size;i++)
            ans[i]=(i^(i>>1));
        return ans;

    }
};

Complexity Analysis

Algorithm Time Complexity Space Complexity
Bit manipulation O(2^N) O(2^N)

Explanation

1. Intuition

2. Implementation

Example

n = 3
size = 1<<3 = 8
ans = [0,0,0,0,0,0,0,0]
i = 0
ans[0] = 0^(0>>1) = 0^0 = 0
i = 1
ans[1] = 1^(1>>1) = 1^0 = 1
i = 2
ans[2] = 2^(2>>1) = 2^1 = 3
i = 3
ans[3] = 3^(3>>1) = 3^1 = 2
i = 4
ans[4] = 4^(4>>1) = 4^2 = 6
i = 5
ans[5] = 5^(5>>1) = 5^2 = 7
i = 6
ans[6] = 6^(6>>1) = 6^3 = 5
i = 7
ans[7] = 7^(7>>1) = 7^3 = 4
ans = [0,1,3,2,6,7,5,4]