Generate all binary numbers in range [L, R] with same length
Last Updated :
23 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. The length of all binary numbers should be same.
Examples:
Input: L = 2, R = 4
Output:
010
011
100
Explanation: The binary representation of the numbers: 2 = 10, 3 = 11 and 4 = 100.
For the numbers to have same length one preceding 0 is added to the binary representation of both 3 and 4.
Input: L = 2, R = 8
Output:
0010
0011
0100
0101
0110
0111
1000
Approach: Follow the approach mentioned below to solve the problem.
- To determine the length of resultant binary numbers, take log of R+1 to the base 2.
- Then traverse from L to R and convert every number to binary of determined length.
- Store each number and print it at the end.
Below is the implementation of the above approach
C++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to convert a number to binary
vector<int> convertToBinary(int num,
int length)
{
vector<int> bits(length, 0);
if (num == 0) {
return bits;
}
int i = length - 1;
while (num != 0) {
bits[i--] = (num % 2);
// Integer division
// gives quotient
num = num / 2;
}
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary of
// same length
vector<vector<int> > getAllBinary(int l,
int r)
{
// Length of the binary numbers
int n = (int) ceil(log(r+1) / log (2));
vector<vector<int> > binary_nos;
for (int i = l; i <= r; i++) {
vector<int> bits =
convertToBinary(i, n);
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 approach
import java.util.*;
public class GFG
{
// Function to convert a number to binary
static ArrayList<Integer> convertToBinary(int num,
int length)
{
ArrayList<Integer> bits= new ArrayList<Integer>();
for(int i = 0; i < length; i++) {
bits.add(0);
}
if (num == 0) {
return bits;
}
int i = length - 1;
while (num != 0) {
bits.set(i, (num % 2));
i = i - 1;
// Integer division
// gives quotient
num = num / 2;
}
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary of
// same length
static ArrayList<ArrayList<Integer> > getAllBinary(int l,
int r)
{
// Length of the binary numbers
double x = Math.log(r+1);
double y = Math.log (2);
int n = (int) Math.ceil(x / y);
ArrayList<ArrayList<Integer> > binary_nos =
new ArrayList<ArrayList<Integer> >();
for (int i = l; i <= r; i++) {
ArrayList<Integer> bits =
convertToBinary(i, n);
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 Samim Hossain Mondal.
Python3
# Python 3 code to implement the approach
import math
# Function to convert a number to binary
def convertToBinary(num, length):
bits = [0]*(length)
if (num == 0):
return bits
i = length - 1
while (num != 0):
bits[i] = (num % 2)
i -= 1
# Integer division
# gives quotient
num = num // 2
return bits
# Function to convert all numbers
# in range [L, R] to binary of
# same length
def getAllBinary(l, r):
# Length of the binary numbers
n = int(math.ceil(math.log(r+1)/ math.log(2)))
binary_nos = []
for i in range(l, r + 1):
bits = convertToBinary(i, n)
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 approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to convert a number to binary
static List<int> convertToBinary(int num, int length)
{
List<int> bits = new List<int>();
int i;
for (i = 0; i < length; i++)
{
bits.Add(0);
}
if (num == 0)
{
return bits;
}
i = length - 1;
while (num != 0)
{
bits[i] = (num % 2);
i = i - 1;
// Integer division
// gives quotient
num = num / 2;
}
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary of
// same length
static List<List<int>> getAllBinary(int l, int r)
{
// Length of the binary numbers
double x = Math.Log(r + 1);
double y = Math.Log(2);
int n = (int)Math.Ceiling(x / y);
List<List<int>> binary_nos = new List<List<int>>();
for (int i = l; i <= r; i++)
{
List<int> bits = convertToBinary(i, n);
binary_nos.Add(bits);
}
return binary_nos;
}
// Driver code
public static void Main()
{
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 Saurabh Jaiswal
JavaScript
<script>
// JavaScript code to implement the approach
// Function to convert a number to binary
const convertToBinary = (num, length) => {
let bits = new Array(length).fill(0);
if (num == 0) {
return bits;
}
let i = length - 1;
while (num != 0) {
bits[i--] = (num % 2);
// Integer division
// gives quotient
num = parseInt(num / 2);
}
return bits;
}
// Function to convert all numbers
// in range [L, R] to binary of
// same length
const getAllBinary = (l, r) => {
// Length of the binary numbers
let n = Math.ceil(Math.log(r + 1) / Math.log(2));
let binary_nos = [];
for (let i = l; i <= r; i++) {
let bits = convertToBinary(i, n);
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>
Output0010
0011
0100
0101
0110
0111
1000
Time Complexity: O(N * logR) where N = (R - L + 1)
Auxiliary Space: O(N * logR)
Similar Reads
Convert all numbers in range [L, R] to binary number 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 = 4Output: 11011100Explanation: The binary representation of the numbers 1, 2, 3 and 4 are: 1 = (1)22 = (10)23 = (11)24 = (100)2 Input: L = 2, R = 8Output:101110
5 min read
Count of numbers with all digits same in a given range Given two integers L and R denoting the starting and end values of a range, the task is to count all numbers in that range whose all digit are same, like 1, 22, 444, 3333, etc.Example: Input: L = 12, R = 68 Output: 5 Explanation: { 22, 33, 44, 55, 66} are the numbers with same digits in the given ra
9 min read
Count of numbers in range [L, R] with LSB as 0 in their Binary representation Given two integers L and R. The task is to find the count of all numbers in the range [L, R] whose Least Significant Bit in binary representation is 0. Examples: Input: L = 10, R = 20 Output: 6 Input: L = 7, R = 11 Output: 2 Naive approach: The simplest approach is to solve this problem is to check
5 min read
Set all the bits in given range of a number Given a non-negative number n and two values l and r. The problem is to set the bits in the range l to r in the binary representation of n, i.e, to unset bits from the rightmost lth bit to the rightmost r-th bit. Constraint: 1 <= l <= r <= number of bits in the binary representation of n.Ex
5 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