Given a number N. The task is to generate and print all binary numbers with decimal values from 1 to N.
Example 1:
Input:
N = 2
Output: 
1 10
Explanation: 
Binary numbers from
1 to 2 are 1 and 10.
Example 2:
Input:
N = 5
Output: 
1 10 11 100 101
Explanation: 
Binary numbers from
1 to 5 are 1 , 10 , 11 , 100 and 101.
 
Your Task:
You only need to complete the function generate() that takes N as parameter and returns vector of strings denoting binary numbers.
Expected Time Complexity : O(N log_{2}N)
Expected Auxilliary Space : O(N log_{2}N)
Constraints:
1 ≤ N ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



#Function to generate binary numbers from 1 to N using a queue.
def generate(N):
    # code here

Solutions:
from queue import Queue

def generate(N):
	queue = Queue(maxsize=0)
	queue.put(1)
	count = 0
	ans = []
	while count < N:
		curr = queue.get()
		ans.append(curr)
		count += 1
		queue.put(curr * 10)
		queue.put(curr * 10 + 1)
	return ans


def generate(N):
	l = []
	for i in range(1, N + 1):
		l.append(bin(i)[2:])
	return l


def generate(N):
	an = []
	for i in range(1, N + 1):
		k = bin(i)[2:]
		an.append(k)
	return an


from collections import *

def generate(N):
	res = []
	dq = deque()
	dq.append('1')
	for i in range(N):
		curr = dq.popleft()
		res.append(curr)
		dq.append(curr + '0')
		dq.append(curr + '1')
	return res


from queue import Queue

def generate(N):
	q = Queue()
	q.put('1')
	res = []
	while N:
		n = q.get()
		n1 = n + '0'
		n2 = n + '1'
		q.put(n1)
		q.put(n2)
		res.append(n)
		N -= 1
	return res


from collections import deque

def generate(n):
	queue = deque()
	queue.append('1')
	ans_list = []
	while not len(queue) == 0:
		num = queue.popleft()
		num1 = num + '0'
		num2 = num + '1'
		ans_list.append(num)
		if int(num1, 2) <= n:
			queue.append(num1)
		if int(num2, 2) <= n:
			queue.append(num2)
	return ans_list


from collections import deque

def generate(N):
	q = deque(['1'])
	res = []
	i = 1
	while i <= N:
		x = q.popleft()
		left = x + '0'
		right = x + '1'
		q.append(left)
		q.append(right)
		res.append(x)
		i = i + 1
	return res


def generate(N):
	sol2 = [''] * N
	for i in range(1, N + 1):
		sol2[i - 1] = to_binary(i)
	return sol2

def to_binary(num):
	sol = []
	while num > 0:
		sol.append(str(num % 2))
		num = num // 2
	return ''.join(sol[::-1])


from collections import deque

def generate(N):
	queue = deque()
	queue.append(1)
	c = 0
	result = []
	while c < N:
		x = queue[0]
		queue.popleft()
		result.append(x)
		queue.append(x * 10)
		queue.append(x * 10 + 1)
		c += 1
	return result


def generate(N):
	l = []
	for n1 in range(1, N + 1):
		re = ''
		n = n1
		while n > 0:
			r = n % 2
			n = n // 2
			re = str(r) + re
		l.append(re)
	return l


def generate(N):
	return [bin(i)[2:] for i in range(1, N + 1)]


def generate(N):
	res = []
	from queue import Queue
	q = Queue()
	q.put('1')
	while N > 0:
		N -= 1
		s1 = q.get()
		res.append(s1)
		s2 = s1
		q.put(s1 + '0')
		q.put(s2 + '1')
	return res


def generate(N):
	r = []
	for i in range(1, n + 1):
		r.append(bin(i)[2:])
	return r


def generate(N):
	q = ['1']
	for i in range(N // 2):
		q.append(q[i] + '0')
		q.append(q[i] + '1')
	return q[:N]


def generate(N):
	lst = []
	for i in range(1, N + 1):
		bi_no = int(bin(i).replace('0b', ''))
		lst.append(bi_no)
	return lst


def binary(n):
	st = ''
	while n != 0:
		t = n & 1
		st += str(t)
		n = n >> 1
	st = st[::-1]
	return st

def generate(N):
	l = []
	for i in range(1, N + 1):
		c = binary(i)
		l.append(c)
	return l


def generate(N):
	li = []
	for i in range(1, N + 1):
		r = ''
		while i != 1:
			r = str(i % 2) + r
			i = i // 2
		r = '1' + r
		li.append(r)
	return li


from collections import deque

def generate(N):
	arr = []
	arr.append('1')
	q = deque('1')
	while q and len(arr) < N:
		s = q.popleft()
		s1 = s + '0'
		s2 = s + '1'
		q.append(s1)
		q.append(s2)
		arr.append(s1)
		arr.append(s2)
	return arr[:N]


def generate(N):
	t = []
	for i in range(1, N + 1):
		s = ''
		a = i
		while a != 0:
			s = str(a % 2) + s
			a = a // 2
		t.append(s)
	return t


def generate(N):
	l1 = []
	for i in range(1, N + 1):
		l1.append(bin(i).replace('0b', ''))
	return l1


from collections import deque

def generate(N):
	stack = deque()
	new = ''
	ans = []
	for i in range(1, N + 1):
		while i != 0:
			temp = i & 1
			stack.append(temp)
			i = i >> 1
		while stack:
			new += str(stack.pop())
		ans.append(new)
		new = ''
	return ans


def generate(N):
	l = []
	for i in range(1, N + 1):
		k = bin(i)
		l.append(''.join(list(k[2:])))
	return l


def generate(N):
	l = []
	for a in range(N):
		l.append(int(bin(a + 1)[2:]))
	return l


def generate(N):
	lis = []
	for i in range(1, N + 1):
		st = bin(i)
		lis.append(st[2:])
	return lis


def generate(N):
	li = []
	for i in range(1, N + 1):
		t = format(i, 'b')
		li.append(t)
	return li


def generate(N):
	res = []
	import queue
	a = queue.Queue()
	a.put('1')
	for i in range(N):
		el = a.get()
		res.append(el)
		a.put(el + '0')
		a.put(el + '1')
	return res


def generate(N):
	l = []
	for i in range(1, N + 1):
		l.append(str(bin(i)).replace('0b', ''))
	return l


def generate(N):
	a = []
	for i in range(1, N + 1):
		ans = binary(i)
		a.append(int(ans))
	return a

def binary(z):
	c = ''
	while z != 0:
		di = z % 2
		c += str(di)
		z //= 2
	c = c[::-1]
	return c


def generate(N):
	arr = []
	queue = []
	queue.append('1')
	for i in range(N):
		res = queue.pop(0)
		queue.append(res + '0')
		queue.append(res + '1')
		arr.append(res)
	return arr


def generate(N):
	a = []
	for i in range(1, N + 1):
		b = bin(i).replace('0b', '')
		a.append(b)
	return a


def generate(N):
	ans = []
	for i in range(1, N + 1):
		st = ''
		while i != 1:
			r = i % 2
			i = i // 2
			st += str(r)
		st = st[::-1]
		st = str(1) + st
		ans.append(st)
	return ans


from queue import Queue

def generate(N):
	q = Queue()
	ans = []
	q.put('1')
	while not q.empty() and N > 0:
		c = q.get()
		ans.append(c)
		q.put(c + '0')
		q.put(c + '1')
		N -= 1
	return ans


def generate(n):
	from queue import Queue
	l = []
	q = Queue()
	q.put('1')
	while n > 0:
		n -= 1
		s1 = q.get()
		l.append(s1)
		s2 = s1
		q.put(s1 + '0')
		q.put(s2 + '1')
	return l


def generate(N):
	(ans, queue) = (['1'], ['1'])
	while len(ans) < N:
		ans.append(queue[0] + '0')
		queue.append(queue[0] + '0')
		ans.append(queue[0] + '1')
		queue.append(queue[0] + '1')
		queue.pop(0)
	if len(ans) > N:
		ans.pop()
	return ans


def generate(N):
	ans = []
	for i in range(1, N + 1):
		a = i
		l = []
		while a != 1:
			temp = a % 2
			a = a // 2
			l.append(str(temp))
		l.append('1')
		l.reverse()
		ans.append(l)
	for i in range(len(ans)):
		ans[i] = ''.join(ans[i])
	return ans


from collections import deque

def generate(N):
	l = []
	for i in range(1, N + 1):
		k = i
		q = deque()
		s = ''
		while k > 0:
			q.append(str(k % 2))
			k = k >> 1
		while q:
			s = q.popleft() + s
		l.append(s)
	return l


from collections import deque

def generate(N):
	queue = deque()
	queue.append('1')
	ans = []
	for i in range(1, N + 1):
		a = queue.popleft()
		ans.append(a)
		num1 = a + '0'
		queue.append(num1)
		num2 = a + '1'
		queue.append(num2)
	return ans


def generate(N):
	temp = ['1']
	res = []
	for i in range(1, N + 1):
		a = temp.pop(0)
		res.append(a)
		temp.append(a + '0')
		temp.append(a + '1')
	return res


def generate(N):
	queue = []
	for i in range(1, n + 1):
		queue.append(bin(i).replace('0b', ''))
	return queue


def generate(N):
	queue = ['1']
	count = 1
	ans = []
	while count <= N:
		num = queue.pop(0)
		ans.append(num)
		x = num + '0'
		queue.append(x)
		y = num + '1'
		queue.append(y)
		count += 1
	return ans


def generate(N):
	lst = []
	for i in range(1, N + 1):
		x = bin(i)[2:]
		lst.append(x)
	return lst


def generate(N):
	queue = []
	l = []
	queue.append('1')
	for _ in range(1, n + 1):
		x = queue.pop(0)
		queue.append(x + '0')
		queue.append(x + '1')
		l.append(x)
	return l


def generate(N):
	r = []
	for i in range(1, N + 1):
		s = str(bin(i))
		r.append(s[2:])
	return r


def generate(N):
	queue = ['1']
	i = 0
	while len(queue) < N:
		A = queue[i]
		queue.append(A + '0')
		if len(queue) == n:
			break
		queue.append(A + '1')
		i = i + 1
	return queue


def generate(N):
	from collections import deque
	q = deque('1')
	l = []
	while N > 0:
		a = q.popleft()
		l.append(a)
		q.append(a + '0')
		q.append(a + '1')
		N -= 1
	return l


def generate(n):
	from collections import deque
	q = deque('1')
	arr = []
	while n:
		x = q.popleft()
		arr.append(x)
		q.append(x + '0')
		q.append(x + '1')
		n -= 1
	return arr


def generate(N):
	ans = ['1']
	for i in ans:
		if N > 1:
			ans.append(i + '0')
			N -= 1
		else:
			break
		if N > 1:
			ans.append(i + '1')
			N -= 1
		else:
			break
	return ans


def generate(N):
	q = []
	l = []
	q.append(1)
	i = 2
	f = 0
	while i <= N + 1:
		temp = q.pop(0)
		l.append(temp)
		q.append(temp * 10)
		q.append(temp * 10 + 1)
		i = i + 1
	return l


from collections import deque

def generate(N):
	q = deque()
	q.append('1')
	ans = []
	for i in range(N):
		a = q.popleft()
		ans.append(a)
		q.append(a + '0')
		q.append(a + '1')
	return ans


from collections import deque

def generate(N):
	q = deque()
	l = []
	q.append('1')
	for i in range(N):
		temp = q.popleft()
		l.append(temp)
		q.append(temp + '0')
		q.append(temp + '1')
	return l


from collections import deque

def generate(N):
	q = ['1']
	for i in range(N // 2):
		q.append(q[i] + '0')
		q.append(q[i] + '1')
	return q[:N]