General Discussions

Printing 1 to N in Binary Using Queue

PH Bytes

One of the popular interview question asked on queue data structure:

Write a program to print the numbers from 1 to N in Binary Representation using QUEUE data structure.

Example:
Input: N = 3
Output: 1, 10, 11

Input: N= 5
Output: 1, 10, 11, 100, 101

Code will follow the following Logic:

  • Initialize queue with 1
  • Repeat this step until required ‘n’ is reached
    • Dequeue front and print
    • Suffix front element with ‘0’ once and ‘1’ another and enqueue both into queue in the same order

One possible C Implementation Solution:

View original post

Advertisements

Let me Know What you Think!

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s