Given a range **[L, R]** where **0 ≤ L ≤ R ≤ 10 ^{8}**. The task is to find the count of integers from the given range that can be represented as

**(2**.

^{x}) * (3^{y})**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 **(2 ^{x}) * (3^{y})** in the range

**[1, 10**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

^{8}]**[L, R]**.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAXI (int)(1e8) ` ` ` `// To store all valid integers ` `vector<` `int` `> v; ` ` ` `// Function to find all the integers of the ` `// form (2^x * 3^y) in the range [0, 1000000] ` `void` `precompute() ` `{ ` ` ` ` ` `// To store powers of 2 and 3 ` ` ` `// initialized to 2^0 and 3^0 ` ` ` `int` `x = 1, y = 1; ` ` ` ` ` `// While current power of 2 ` ` ` `// is within the range ` ` ` `while` `(x <= MAXI) { ` ` ` ` ` `// While number is within the range ` ` ` `while` `(x * y <= MAXI) { ` ` ` ` ` `// Add the number to the vector ` ` ` `v.push_back(x * y); ` ` ` ` ` `// Next power of 3 ` ` ` `y *= 3; ` ` ` `} ` ` ` ` ` `// Next power of 2 ` ` ` `x *= 2; ` ` ` ` ` `// Reset to 3^0 ` ` ` `y = 1; ` ` ` `} ` ` ` ` ` `// Sort the vector ` ` ` `sort(v.begin(), v.end()); ` `} ` ` ` `// Function to find the count of numbers ` `// in the range [l, r] which are ` `// of the form (2^x * 3^y) ` `void` `countNum(` `int` `l, ` `int` `r) ` `{ ` ` ` `cout << upper_bound(v.begin(), v.end(), r) ` ` ` `- upper_bound(v.begin(), v.end(), l - 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 100, r = 200; ` ` ` ` ` `// Pre-compute all the valid numbers ` ` ` `precompute(); ` ` ` ` ` `countNum(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

5

## Recommended Posts:

- Ways to form an array having integers in given range such that total sum is divisible by 2
- Count integers in the range [A, B] that are not divisible by C and D
- Count integers in a range which are divisible by their euler totient value
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Most frequent factor in a range of integers
- Find sum in range L to R in given sequence of integers
- Range Queries to count elements lying in a given Range : MO's Algorithm
- Find pair with maximum GCD for integers in range 2 to N
- Find Prime Adam integers in the given range [L, R]
- Number of integers in a range [L, R] which are divisible by exactly K of it's digits
- Integers from the range that are composed of a single distinct digit
- Given an array and two integers l and r, find the kth largest element in the range [l, r]
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count of integers up to N which are non divisors and non coprime with N
- Sum of product of all integers upto N with their count of divisors
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Count of m digit integers that are divisible by an integer n
- Count of digits after concatenation of first N positive integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.