Count the possible integer sets of maximum size with the given condition
Given two integers L and R, the task is to find the count of the sets of a maximum size such that each element in the set is between L and R (inclusive), and for any two elements in the set one of them is divisible by the other.
Examples:
Input: L = 3, R = 19
Output: 4
Explanation: 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
Approach: This can be solved with the following idea:
- Let {S1, S2, S3…… Smx} be a set of maximum sizes satisfying the given conditions. Let Mi = Si+1/Si.
- It is intuitive that for Smx to be the minimum we need to choose S1 and Mi for all i as low as possible, the minimum value of S1 can be L and the minimum value of Mi can be 2.
- However, we can choose one of the Mi to be 3 so that Smx will be (3/2) times the initial value of Smx (which should be less than R), if we choose any value of Mi to be more than 3, the size of the set would not be maximum as there can always be a new element Smx+1 = 2*Smx such size of the set would become mx+1.
Follow the below steps to implement the idea:
- First calculate the value of mx, i.e the maximum possible size of the set. This can be calculated assuming all the values of Mi are 2, and the value of S1 is L, then mx = floor(log2(r/l)) + 1.
- Calculate the maximum value of S1 such that a set of size mx satisfying the given conditions is possible. Let’s call it X, We know 2mx-1 * X ≤ R, then X = R/2mx-1.
- Calculate the maximum value of S1 such that a set of size mx satisfying the given conditions is possible and one of the values of Mi can be 3 instead of 2, let us call it Y. We know that 3*2mx-2 * Y ≤ R, then Y = R/(3*2mx-2).
- We know L ≤ Y ≤ X ≤ R, a number of sets with S1 ≤ Y are (Y-L+1)*mx, note that we multiplied by mx as any of the Mi in these sets can be 3. A number of sets with S1>Y and S1 ≤ X is X – Y.
- Total sets of maximum size = (Y-L+1)*mx + X-Y.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSets( int L, int R)
{
int mx = floor (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 main()
{
int L = 3, R = 19;
cout << countSets(L, R) << endl;
}
|
Java
import java.util.*;
public class Main {
static int countSets( int L, int R) {
int mx = ( int ) Math.floor(Math.log(R / L) / Math.log( 2 )) + 1 ;
if (mx == 1 ) {
return (R - L + 1 );
}
int X = ( int ) (R / Math.pow( 2 , mx - 1 ));
int Y = ( int ) (R / (Math.pow( 2 , mx - 2 ) * 3 ));
if (Y < L) {
return (X - L + 1 );
}
int ans = (Y - L + 1 ) * mx + (X - Y);
return ans;
}
public static void main(String[] args) {
int L = 3 , R = 19 ;
System.out.println(countSets(L, R));
}
}
|
Python3
import math
def countSets(L, R):
mx = int (math.floor(math.log(R / L) / math.log( 2 ))) + 1
if mx = = 1 :
return (R - L + 1 )
X = int (R / ( 2 * * (mx - 1 )))
Y = int (R / (( 2 * * (mx - 2 )) * 3 ))
if Y < L:
return (X - L + 1 )
ans = (Y - L + 1 ) * mx + (X - Y)
return ans
L = 3
R = 19
print (countSets(L, R))
|
C#
using System;
public class GFG {
static int countSets( int L, int R)
{
int mx
= ( int )Math.Floor(Math.Log(R / L) / Math.Log(2))
+ 1;
if (mx == 1) {
return (R - L + 1);
}
int X = ( int )(R / Math.Pow(2, mx - 1));
int Y = ( int )(R / (Math.Pow(2, mx - 2) * 3));
if (Y < L) {
return (X - L + 1);
}
int ans = (Y - L + 1) * mx + (X - Y);
return ans;
}
static public void Main()
{
int L = 3, R = 19;
Console.WriteLine(countSets(L, R));
}
}
|
Javascript
function countSets(L, R) {
let mx = Math.floor(Math.log2(R / L)) + 1;
if (mx === 1) {
return R - L + 1;
}
let X = Math.floor(R / Math.pow(2, mx - 1));
let Y = Math.floor(R / (Math.pow(2, mx - 2) * 3));
if (Y < L) {
return X - L + 1;
}
let ans = (Y - L + 1) * mx + (X - Y);
return ans;
}
let L = 3, R = 19;
console.log(countSets(L, R));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
07 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...