Generate Binary Numbers
Posted: 16 Dec, 2020
Your friend Ninja has been learning about binary numbers lately. In order to understand binary numbers with perfection, Ninja asks you to generate a list of binary numbers from 1 to ‘N’, which he can use later for reference.
For every integer Ninja gives, your task is to generate all the binary numbers from 1 to ‘N’.
Consider N = 5, All the binary numbers from 1 to 5 are: 1, 10, 11, 100, 101.
The very first line of input contains an integer ‘T’ denoting the number of test cases. The first and the only line of every test case contains a positive integer ‘N’.
For each test case, print ‘N’ space-separated binary numbers from 1 to ‘N’, in a separate line. Print the output of each test case in a separate line.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10 1 <= N <= 10 ^ 5 Time Limit: 1 sec
The idea is to generate all the decimal numbers from 1 to N and convert each of them to their corresponding binary representation.
This can be done as follows:
- Loop through all the decimal numbers from 1 to N.
- During each iteration, convert the decimal number to its corresponding binary representation.
- The algorithm to convert a decimal number to binary is as follows:
- Divide the number by 2 and store its remainder.
- Repeat the previous step until the number is greater than 0.
- Now, create a string of the remainders in reverse of the order in which they were obtained.
- Print the string, which is the binary representation of your decimal number.
Game of 3
Posted: 11 Jul, 2021
Lexicographic Permutation Rank
Posted: 13 Jul, 2021
Zero Pair Sum
Posted: 22 Jul, 2021
Implement a Queue
Posted: 27 Jul, 2021
Remove K Corner Elements
Posted: 31 Jul, 2021