Count of integers of the form (2^x * 3^y) in the range [L, R]
Last Updated :
29 Aug, 2022
Given a range [L, R] where 0 ? L ? R ? 108. The task is to find the count of integers from the given range that can be represented as (2x) * (3y).
Examples:
Input: L = 1, R = 10
Output: 7
The numbers are 1, 2, 3, 4, 6, 8 and 9
Input: L = 100, R = 200
Output: 5
The numbers are 108, 128, 144, 162 and 192
Approach: Since the numbers, which are powers of two and three, quickly grow, you can use the following algorithm. For all the numbers of the form (2x) * (3y) in the range [1, 108] store them in a vector. Later sort the vector. Then the required answer can be calculated using an upper bound. Pre-calculating these integers will be helpful when there are a number of queries of the form [L, R].
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
#define MAXI (int)(1e8)
vector< int > v;
void precompute()
{
int x = 1, y = 1;
while (x <= MAXI) {
while (x * y <= MAXI) {
v.push_back(x * y);
y *= 3;
}
x *= 2;
y = 1;
}
sort(v.begin(), v.end());
}
void countNum( int l, int r)
{
cout << upper_bound(v.begin(), v.end(), r)
- upper_bound(v.begin(), v.end(), l - 1);
}
int main()
{
int l = 100, r = 200;
precompute();
countNum(l, r);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int MAXI = 100000000 ;
static ArrayList<Integer> v = new ArrayList<Integer>();
static int upper_bound(ArrayList<Integer> arr, int elem)
{
for (var i = 0 ; i < arr.size(); i++)
if (elem < arr.get(i))
return i;
return arr.size();
}
static void precompute()
{
int x = 1 , y = 1 ;
while (x <= MAXI) {
while (x * y <= MAXI) {
v.add(x * y);
y *= 3 ;
}
x *= 2 ;
y = 1 ;
}
Collections.sort(v);
}
static void countNum( int l, int r)
{
System.out.println(upper_bound(v, r)
- upper_bound(v, l - 1 ));
}
public static void main(String[] args)
{
int l = 100 , r = 200 ;
precompute();
countNum(l, r);
}
}
|
Python3
import bisect
MAXI = int ( 1e8 )
v = []
def precompute():
x = 1 ; y = 1
while (x < = MAXI) :
while (x * y < = MAXI) :
v.append(x * y)
y * = 3
x * = 2
y = 1
v.sort()
def countNum(l, r):
print (bisect.bisect_right(v, r) - bisect.bisect_right(v,l - 1 ))
if __name__ = = '__main__' :
l = 100 ; r = 200
precompute()
countNum(l, r)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MAXI = 100000000;
static List< int > v = new List< int >();
static int upper_bound(List< int > arr, int elem)
{
for ( var i = 0; i < arr.Count; i++)
if (elem < arr[i])
return i;
return arr.Count;
}
static void precompute()
{
int x = 1, y = 1;
while (x <= MAXI) {
while (x * y <= MAXI) {
v.Add(x * y);
y *= 3;
}
x *= 2;
y = 1;
}
v.Sort();
}
static void countNum( int l, int r)
{
Console.WriteLine(upper_bound(v, r)
- upper_bound(v, l - 1));
}
public static void Main( string [] args)
{
int l = 100, r = 200;
precompute();
countNum(l, r);
}
}
|
Javascript
let MAXI = 100000000
let v = [];
function upper_bound(arr, elem)
{
for ( var i = 0; i < arr.length; i++)
if (elem < arr[i])
return i
return arr.length;
}
function precompute()
{
let x = 1, y = 1;
while (x <= MAXI) {
while (x * y <= MAXI) {
v.push(parseInt(x * y));
y *= 3;
}
x *= 2;
y = 1;
}
v.sort( function (a, b) {
return a - b;
});
}
function countNum(l, r)
{
console.log(upper_bound(v, r) - upper_bound(v, l - 1));
}
let l = 100, r = 200;
precompute();
countNum(l, r);
|
Time Complexity: O(N * log(N)), where N = logX + logY
Auxiliary Space: O(N), as N extra space has been taken.
Share your thoughts in the comments
Please Login to comment...