Rely the potential integer units of most dimension with the given situation



Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given two integers L and R,   the duty is to search out the depend of the units of a most dimension such that every ingredient within the set is between L and R (inclusive), and for any two components within the set one among them is divisible by the opposite.


Enter: L = 3, R = 19
Output: 4
Clarification: There will likely be 4 potential units – {3, 6, 12}, {3, 6, 18}, {3, 9, 18}, {4, 8, 16}

Enter: L = 4, R = 8
Output: 1

Method: This may be solved with the next concept:

  • Let {S1, S2, S3…… Smx} be a set of most sizes satisfying the given circumstances. Let Mi = Si+1/Si. 
  • It’s intuitive that for Smx to be the minimal we have to select S1 and Mi for all i as little as potential, the minimal worth of S1 will be L and the minimal worth of Mi will be 2
  • Nonetheless, we will select one of many Mi to be 3 in order that Smx will likely be (3/2) instances the preliminary worth of Smx (which needs to be lower than R), if we select any worth of Mi to be greater than 3, the scale of the set wouldn’t be most as there can all the time be a brand new ingredient Smx+1  = 2*Smx such dimension of the set would turn out to be mx+1. 

Comply with the under steps to implement the concept: 

  • First calculate the worth of mx, i.e the utmost potential dimension of the set. This may be calculated assuming all of the values of Mi are 2, and the worth of S1 is L, then mx = flooring(log2(r/l)) + 1
  • Calculate the utmost worth of S1 such {that a} set of dimension mx satisfying the given circumstances is feasible. Let’s name it X, We all know 2mx-1 * X ≤ R, then X = R/2mx-1.
  • Calculate the utmost worth of S1 such {that a} set of dimension mx satisfying the given circumstances is feasible and one of many values of Mi will be 3 as a substitute of two, allow us to name it Y. We all know that 3*2mx-2 * Y ≤ R, then Y = R/(3*2mx-2).
  • We all know L ≤ Y ≤ X ≤ R, various units with S1 ≤ Y are (Y-L+1)*mx, observe that we multiplied by mx as any of the Mi in these units will be 3. Plenty of units with S1>Y and S1 ≤ X is X – Y.
  • Whole units of most dimension = (Y-L+1)*mx + X-Y.

Under is the implementation of the above strategy:


#embrace <bits/stdc++.h>

utilizing namespace std;


int countSets(int L, int R)





    int mx = flooring(log2(R / L)) + 1;





    if (mx == 1) {

        return (R - L + 1);




    int X = R / pow(2, mx - 1);

    int Y = R / (pow(2, mx - 2) * 3);




    if (Y < L) {

        return (X - L + 1);




    int ans = (Y - L + 1) * mx + (X - Y);


    return ans;



int primary()


    int L = 3, R = 19;



    cout << countSets(L, R) << endl;


Time Complexity: O(1) 
Auxilairy House: O(1)