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.
C++
// C++ code to implement the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to convert a number
// to its binary equivalent
vector<int> convertToBinary(int num)
{
vector<int> bits;
if (num == 0) {
bits.push_back(0);
return bits;
}
while (num != 0) {
bits.push_back(num % 2);
// Integer division
// gives quotient
num = num / 2;
}
reverse(bits.begin(), bits.end());
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary
vector<vector<int> > getAllBinary(int l,
int r)
{
// Vector to store the binary
// representations of the numbers
vector<vector<int> > binary_nos;
for (int i = l; i <= r; i++) {
vector<int> bits =
convertToBinary(i);
binary_nos.push_back(bits);
}
return binary_nos;
}
// Driver code
int main()
{
int L = 2, R = 8;
vector<vector<int> > binary_nos =
getAllBinary(L, R);
for (int i = 0; i < binary_nos.size();
i++) {
for (int j = 0; j < binary_nos[i].size();
j++)
cout << binary_nos[i][j];
cout << endl;
}
return 0;
}
Java
// Java code to implement the above approach
import java.util.ArrayList;
import java.util.Collections;
class GFG {
// Function to convert a number
// to its binary equivalent
public static ArrayList<Integer> convertToBinary(int num) {
ArrayList<Integer> bits = new ArrayList<Integer>();
if (num == 0) {
bits.add(0);
return bits;
}
while (num != 0) {
bits.add(num % 2);
// Integer division
// gives quotient
num = num / 2;
}
Collections.reverse(bits);
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary
public static ArrayList<ArrayList<Integer>> getAllBinary(int l, int r)
{
// Vector to store the binary
// representations of the numbers
ArrayList<ArrayList<Integer>> binary_nos = new ArrayList<ArrayList<Integer>>();
for (int i = l; i <= r; i++)
{
ArrayList<Integer> bits = convertToBinary(i);
binary_nos.add(bits);
}
return binary_nos;
}
// Driver code
public static void main(String args[])
{
int L = 2, R = 8;
ArrayList<ArrayList<Integer>> binary_nos = getAllBinary(L, R);
for (int i = 0; i < binary_nos.size(); i++) {
for (int j = 0; j < binary_nos.get(i).size(); j++)
System.out.print(binary_nos.get(i).get(j));
System.out.println("");
}
}
}
// This code is contributed by saurabh_jaiswal.
Python3
# Python 3 code to implement the above approach
# Function to convert a number
# to its binary equivalent
def convertToBinary(num):
bits = []
if (num == 0):
bits.append(0)
return bits
while (num != 0):
bits.append(num % 2)
# Integer division
# gives quotient
num = num // 2
bits.reverse()
return bits
# Function to convert all numbers
# in range [L, R] to binary
def getAllBinary(l, r):
# Vector to store the binary
# representations of the numbers
binary_nos = []
for i in range(l, r+1):
bits = convertToBinary(i)
binary_nos.append(bits)
return binary_nos
# Driver code
if __name__ == "__main__":
L = 2
R = 8
binary_nos = getAllBinary(L, R)
for i in range(len(binary_nos)):
for j in range(len(binary_nos[i])):
print(binary_nos[i][j], end="")
print()
# This code is contributed by ukasp.
C#
// C# code to implement the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to convert a number
// to its binary equivalent
public static List<int> convertToBinary(int num) {
List<int> bits = new List<int>();
if (num == 0) {
bits.Add(0);
return bits;
}
while (num != 0) {
bits.Add(num % 2);
// int division
// gives quotient
num = num / 2;
}
bits.Reverse();
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary
public static List<List<int>> getAllBinary(int l, int r)
{
// List to store the binary
// representations of the numbers
List<List<int>> binary_nos = new List<List<int>>();
for (int i = l; i <= r; i++)
{
List<int> bits = convertToBinary(i);
binary_nos.Add(bits);
}
return binary_nos;
}
// Driver code
public static void Main(String []args)
{
int L = 2, R = 8;
List<List<int>> binary_nos = getAllBinary(L, R);
for (int i = 0; i < binary_nos.Count; i++) {
for (int j = 0; j < binary_nos[i].Count; j++)
Console.Write(binary_nos[i][j]);
Console.WriteLine("");
}
}
}
// This code is contributed by 29AjayKumar
JavaScript
<script>
// JavaScript code to implement the above approach
// Function to convert a number
// to its binary equivalent
const convertToBinary = (num) => {
let bits = [];
if (num == 0) {
bits.push(0);
return bits;
}
while (num != 0) {
bits.push(num % 2);
// Integer division
// gives quotient
num = parseInt(num / 2);
}
bits.reverse()
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary
const getAllBinary = (l, r) => {
// Vector to store the binary
// representations of the numbers
let binary_nos = [];
for (let i = l; i <= r; i++) {
let bits = convertToBinary(i);
binary_nos.push(bits);
}
return binary_nos;
}
// Driver code
let L = 2, R = 8;
let binary_nos = getAllBinary(L, R);
for (let i = 0; i < binary_nos.length;
i++) {
for (let j = 0; j < binary_nos[i].length;
j++)
document.write(`${binary_nos[i][j]}`);
document.write("<br/>");
}
// This code is contributed by rakeshsahni
</script>
Output10
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)
Similar Reads
Generate all binary numbers in range [L, R] with same length Given two positive integer numbers L and R. The task is to convert all the numbers from L to R to binary number. The length of all binary numbers should be same. Examples: Input: L = 2, R = 4Output:010011100Explanation: The binary representation of the numbers: 2 = 10, 3 = 11 and 4 = 100.For the num
6 min read
Program to Convert Octal Number to Binary Number Given an Octal number as input, the task is to convert that number to Binary number. Examples: Input : Octal = 345Output : Binary = 11100101Explanation : Equivalent binary value of 3: 011Equivalent binary value of 4: 100Equivalent binary value of 5: 101 Input : Octal = 120Output : Binary = 1010000 O
9 min read
Count total set bits in all numbers from range L to R Given two positive integers L and R, the task is to count the total number of set bits in the binary representation of all the numbers from L to R. Examples: Input: L = 3, R = 5 Output: 5 Explanation: (3)10 = (11)2, (4)10 = (100)2, (5)10 = (101)2 So, Total set bit in range 3 to 5 is 5 Input: L = 10,
15+ min read
XOR of very large Binary Numbers in range [L, R] Given two binary strings L and R, the task is to find the xor from L to R. Length of the binary string is < = 10e6. Examples: Input: L = 10, R = 100Output: 101Explanation: L = 2 and R = 4 in decimal system.Therefore, the xor will be 2^3^4 = 5(101). Input: L = 10, R = 10000Output: 10001 Naive Appr
15+ min read
Generate all the binary number from 0 to n Given a positive integer number n generate all the binary number from 0 to n. Examples: Input : 5 Output : 0 1 10 11 100 101 Binary numbers are 0(0), 1(1), 2(10), 3(11), 4(100) and 5(101). Input : 10 Output : 0 1 10 11 100 101 110 111 1000 1001 1010 This program simple use predefined function (itoa(
6 min read