A rotating beacon steps through the standard reflected binary Gray code: an ordering of the non-negative integers in which consecutive entries differ in exactly one bit. The sequence begins 0, 1, 3, 2, 6, 7, 5, 4, ... and is defined recursively by reflection (list the first block, then the same block reversed with the next-higher bit prepended).
Given a 0-indexed position k, output the value that sits at position k of this sequence.
Line 1: an integer k.
A single integer: the k-th value of the reflected Gray code sequence.
Example 1
Input
5
Expected
7
Explanation
The sequence is 0,1,3,2,6,7,5,4; position 5 (0-indexed) holds 7.
Example 2
Input
0
Expected
0
Explanation
The sequence starts at 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →