VOOZH about

URL: https://www.geeksforgeeks.org/dsa/convert-all-numbers-in-range-l-r-to-binary-number/

⇱ Convert all numbers in range [L, R] to binary number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Convert all numbers in range [L, R] to binary number

Last Updated : 24 Dec, 2021

Given two positive integer numbers L and R. The task is to convert all the numbers from L to R to binary number. 

Examples:

Input: L = 1, R = 4
Output: 
1
10
11
100
Explanation: The binary representation of the numbers 1, 2, 3 and 4 are: 
1 = (1)2
2 = (10)2
3 = (11)2
4 = (100)2

Input: L = 2, R = 8
Output:
10
11
100
101
110
111
1000

Approach: The problem can be solved using following approach.

  • Traverse from L to R and convert every number to binary number.
  • Store each number and print it at the end.

Below is the implementation of the above approach.

 
 


Output
10
11
100
101
110
111
1000


 

Time Complexity: O(N * LogR) Where N is the count of numbers in range [L, R]
Auxiliary Space: O(N * logR)


 

Comment