Count the possible integer sets of optimum size with the offered condition

Enhance Post

Conserve Post

Like Post

Enhance Post

Conserve Post

Offered 2 integers L and R, the job is to discover the count of the sets of a optimum size such that each component in the set is in between L and R (inclusive), and for any 2 components in the set among them is divisible by the other.

Examples:

Input: L = 3, R = 19
Output: 4
Description: There will be 4 possible sets– {3, 6, 12}, {3, 6, 18}, {3, 9, 18}, {4, 8, 16}

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

Method: This can be resolved with the following concept:

  • Let {S 1, S 2, S 3.. S mx} be a set of optimum sizes pleasing the offered conditions. Let M i = S i +1/ S i.
  • It is instinctive that for S mx to be the minimum we require to pick S 1 and M i for all i as low as possible, the minimum worth of S 1 can be L and the minimum worth of M i can be 2
  • Nevertheless, we can pick among the M i to be 3 so that S mx will be ( 3/2) times the preliminary worth of S mx (which must be less than R), if we pick any worth of M i to be more than 3, the size of the set would not be optimal as there can constantly be a brand-new component S mx +1= 2 * S mx such size of the set would end up being mx +1.

Follow the listed below actions to execute the concept:

  • Very first compute the worth of mx, i.e the optimum possible size of the set. This can be determined presuming all the worths of M i are 2, and the worth of S 1 is L, then mx = flooring( log2( r/l)) + 1
  • Determine the optimum worth of S 1 such that a set of size mx pleasing the offered conditions is possible. Let’s call it X, We understand 2 mx-1 * X ≤ R, then X = R/2 mx-1
  • Determine the optimum worth of S 1 such that a set of size mx pleasing the offered conditions is possible and among the worths of M i can be 3 rather of 2, let us call it Y We understand that 3 * 2 mx-2 * Y ≤ R, then Y = R/( 3 * 2 mx-2)
  • We understand L ≤ Y ≤ X ≤ R, a variety of sets with S 1 ≤ Y are ( Y-L +1) * mx, note that we increased by mx as any of the M i in these sets can be 3. A variety of sets with S 1>> Y and S 1 ≤ X is X– Y.
  • Overall sets of optimum size = ( Y-L +1) * mx + X-Y.

Below is the application of the above method:

C++

#include << bits/stdc++. h>>

utilizing namespace sexually transmitted disease;

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 Intricacy: O( 1 )
Auxilairy Area: O( 1 )

Like this post? Please share to your friends:
Leave a Reply

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!: