`{"id":"6e216d9a-b013-464f-8daf-686b32a6c0e1","name":"Gray Code","description":"1. The gray code is a binary numeral system where two successive numbers differ in only one bit.\r\n2. Given a non-negative integer n representing the total number of bits in the code, print the \r\n sequence of gray code. A gray code sequence must begin with 0.\r\n \r\n Example:\r\n Input: 2\r\n Output: [0,1,3,2]\r\n Explanation:\r\n 00 - 0\r\n 01 - 1\r\n 11 - 3\r\n 10 - 2\r\n [0,2,3,1] is also a valid gray code sequence.\r\n 00 - 0\r\n 10 - 2\r\n 11 - 3\r\n 01 - 1","inputFormat":"First line contains n(number of digits).","outputFormat":"Return the list of numbers in any valid order.","constraints":"0&lt;=n&lt;=20","sampleCode":{"cpp":{"code":"#include<iostream>\r\n #include<vector>\r\n using namespace std;\r\n \r\n vector<int> grayCode(int n) {\r\n \t //Write your code here\r\n }\r\n \r\n \r\n int main(int argc, char** argv){\r\n int n;\r\n cin>>n;\r\n vector<int> res = grayCode(n);\r\n cout<<\"[\";\r\n for(int i=0; i<res.size(); i++){\r\n cout<<res[i];\r\n if(i != res.size() - 1){\r\n cout<<\", \";\r\n }\r\n }\r\n cout<<\"]\";\r\n }"},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n public static List<Integer> grayCode(int n) {\r\n \t //Write your code here\r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n List<Integer> ans=grayCode(scn.nextInt());\r\n Collections.sort(ans);\r\n System.out.println(ans);\r\n }\r\n }"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2","sampleOutput":"[0, 1, 2, 3]","questionVideo":"https://www.youtube.com/embed/KOD2BFauQbA?end=77","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"88cf0a11-fda9-4cf0-8368-ab5fe8f9b6a1","name":"Gray Code","slug":"gray-code","type":1}],"next":{"id":"fd8f6680-6d0c-49fb-a01c-89da6e56e3ba","name":"Gray Code MCQ","type":0,"slug":"gray-code-mcq"},"prev":{"id":"88387c7d-c1bd-4ac4-b87b-6858a869f584","name":"Josephus Special","type":3,"slug":"josephus-special"}}}`

# Gray Code

1. The gray code is a binary numeral system where two successive numbers differ in only one bit. 2. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Example: Input: 2 Output: [0,1,3,2] Explanation: 00 - 0 01 - 1 11 - 3 10 - 2 [0,2,3,1] is also a valid gray code sequence. 00 - 0 10 - 2 11 - 3 01 - 1

`{"id":"6e216d9a-b013-464f-8daf-686b32a6c0e1","name":"Gray Code","description":"1. The gray code is a binary numeral system where two successive numbers differ in only one bit.\r\n2. Given a non-negative integer n representing the total number of bits in the code, print the \r\n sequence of gray code. A gray code sequence must begin with 0.\r\n \r\n Example:\r\n Input: 2\r\n Output: [0,1,3,2]\r\n Explanation:\r\n 00 - 0\r\n 01 - 1\r\n 11 - 3\r\n 10 - 2\r\n [0,2,3,1] is also a valid gray code sequence.\r\n 00 - 0\r\n 10 - 2\r\n 11 - 3\r\n 01 - 1","inputFormat":"First line contains n(number of digits).","outputFormat":"Return the list of numbers in any valid order.","constraints":"0&lt;=n&lt;=20","sampleCode":{"cpp":{"code":"#include<iostream>\r\n #include<vector>\r\n using namespace std;\r\n \r\n vector<int> grayCode(int n) {\r\n \t //Write your code here\r\n }\r\n \r\n \r\n int main(int argc, char** argv){\r\n int n;\r\n cin>>n;\r\n vector<int> res = grayCode(n);\r\n cout<<\"[\";\r\n for(int i=0; i<res.size(); i++){\r\n cout<<res[i];\r\n if(i != res.size() - 1){\r\n cout<<\", \";\r\n }\r\n }\r\n cout<<\"]\";\r\n }"},"java":{"code":"import java.util.*;\r\n \r\n public class Main {\r\n \r\n public static List<Integer> grayCode(int n) {\r\n \t //Write your code here\r\n }\r\n \r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n List<Integer> ans=grayCode(scn.nextInt());\r\n Collections.sort(ans);\r\n System.out.println(ans);\r\n }\r\n }"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2","sampleOutput":"[0, 1, 2, 3]","questionVideo":"https://www.youtube.com/embed/KOD2BFauQbA?end=77","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"88cf0a11-fda9-4cf0-8368-ab5fe8f9b6a1","name":"Gray Code","slug":"gray-code","type":1}],"next":{"id":"fd8f6680-6d0c-49fb-a01c-89da6e56e3ba","name":"Gray Code MCQ","type":0,"slug":"gray-code-mcq"},"prev":{"id":"88387c7d-c1bd-4ac4-b87b-6858a869f584","name":"Josephus Special","type":3,"slug":"josephus-special"}}}`

Editor

# Gray Code

easy

1. The gray code is a binary numeral system where two successive numbers differ in only one bit. 2. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0. Example: Input: 2 Output: [0,1,3,2] Explanation: 00 - 0 01 - 1 11 - 3 10 - 2 [0,2,3,1] is also a valid gray code sequence. 00 - 0 10 - 2 11 - 3 01 - 1

0<=n<=20

## Format

### Input

First line contains n(number of digits).

### Output

Return the list of numbers in any valid order.

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}2`

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}[0, 1, 2, 3]`

Question Video

Discussions

Show Discussion

Related Resources