Kumar-laxmi / Algorithms

A Repository for algorithms in C, C++, Python and Java
Apache License 2.0
326 stars 367 forks source link

Adding an Algorithm to generate n-bit Gray Codes #1744

Closed Inferno2211 closed 8 months ago

Inferno2211 commented 1 year ago

Is your feature request related to a problem? Please describe. Yes, I would like to add a binary algorithm, Gray Codes, in Python, Java, C and C++

Given a number N, generate bit patterns from 0 to 2^N-1 such that successive patterns differ by one bit.

Describe the solution you'd like n-bit Gray Codes can be generated from list of (n-1)-bit Gray codes using following steps.

Let the list of (n-1)-bit Gray codes be L1. Create another list L2 which is reverse of L1. Modify the list L1 by prefixing a ‘0’ in all codes of L1. Modify the list L2 by prefixing a ‘1’ in all codes of L2. Concatenate L1 and L2. The concatenated list is required list of n-bit Gray codes

Additional context Input: N = 3 Output: 000 001 011 010 110 111 101 100

Inferno2211 commented 1 year ago

@Kumar-laxmi Please assign me this issue under SSoC'23 I can add the algorithm in all 4 languages

github-actions[bot] commented 8 months ago

Stale issue message