1359. Find Nth Gray Code Number

MediumBit ManipulationGray CodeBit ManipulationFormula

Return the n-th value (0-indexed) of the reflected Gray code sequence, that is the number whose binary representation differs from the (n-1)-th value in exactly one bit.

Input: A JSON object {"n": <index into the Gray sequence>}.

Output: Return the n-th Gray code value.

Examples

Example 1
Input: {"n":5}
Output: 7
Explanation: 5 XOR 2 = 7.
Example 2
Input: {"n":9}
Output: 13
Explanation: 9 XOR 4 = 13.

Constraints

Asked by

AmazonMicrosoftGoogleAdobeFlipkart
Solve this problem in the editor →