Opening the reading…
Opening the reading…
BIT MANIPULATION › BITWISE XOR OPERATOR
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Try it yourself first →A Gray code changes one bit at a time while counting through all n-bit values. Start with the ordinary binary count i. When you compare i with the value immediately before it, the bits that change form a special pattern: the lowest changed bit is the rightmost 1 in i, and every lower bit resets. Gray code removes the unnecessary lower-bit changes by combining i with a shifted copy of itself.
For each index i, compute i ^ (i >> 1). The shifted copy lines up each bit with the bit to its left, so XOR keeps exactly the boundary where the binary count changes from one region to the next. Consecutive transformed values therefore differ in one bit. The transformation also begins at 0, and the final transformed value is 1, so the sequence is cyclic because its last value differs from its first by one bit.
| MEASURE | BOUND | WHY |
|---|---|---|
| Time | O(2^n) | The loop performs one constant-time shift and XOR for each of the 2^n required output positions. Every position is generated once, so there is no repeated construction or comparison. |
| Space | O(1) extra | The result array is required output and is excluded from the extra-space bound. Apart from it, the algorithm stores only total, i, and the current Gray code value, so the working memory stays constant even when n reaches its largest allowed value. |
#include <vector>
using namespace std;
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result;
int total = 1 << n;
for (int i = 0; i < total; ++i) {
result.push_back(i ^ (i >> 1));
}
return result;
}
};The loop index is not itself a Gray code value; it is the position in the answer. The expression i >> 1 copies each bit one position to the right, and XOR compares the original and shifted copies. That single expression is why the code does not need to inspect adjacent values or repair the final pair afterward.
You can also build the sequence by extending a smaller Gray code. If a valid sequence uses n - 1 bits, keep it as the first half, then traverse it backward while adding 2^(n - 1) to every value. The added high bit distinguishes the second half, while reversing the old sequence makes the boundary between neighboring values differ only in that high bit.
#include <vector>
using namespace std;
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> result{0};
for (int bits = 0; bits < n; ++bits) {
int highestBit = 1 << bits;
int currentSize = static_cast<int>(result.size());
for (int i = currentSize - 1; i >= 0; --i) {
result.push_back(result[i] | highestBit);
}
}
return result;
}
};This is not a time or output-space optimisation: it still performs O(2^n) work and stores O(2^n) output. It is a different arrangement of the same structure, and it can be easier to prove from the construction itself. The direct formula is shorter and uses constant extra working memory, while this version shows exactly why the reflected boundary has one changed bit.