Generate Grey Code Sequences
Easy Accuracy: 46.62% Submissions: 4296 Points: 2

Given a number N, your task is to complete the function which generates all n-bit grey code sequences, a grey code sequence is a sequence such that successive patterns in it differ by one bit.

Example 1:

Input:
N = 2
Output: 00 01 11 10
Explanation: All 2-bit gray codes are
00, 01, 11, 10 such that successive
patterns in it differ by one bit.

Example 2:

Input:
N = 1
Output: 0 1

 

Your Task:
Complete the function generateCode() which takes an integer as input parameter and retruns an array of all N bit grey code sequence such that successive patterns in it differ by one bit.

Expected Time Complexity: O(N * log N)
Expected Auxiliary Space: O(1)

Constraints:
1<=N<=10

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.