Last Updated: 17 Mar, 2021

Glowing Bulbs

Hard
Asked in companies
TCSJosh Technology Group

Problem statement

There are an infinite number of electric bulbs. Each bulb is assigned a unique integer starting from 1. There are ‘N’ switches also and each switch is labeled by a unique prime number. If a switch labeled with prime integer ‘p’ is turned ON, then all the bulbs having a number that is multiple of ‘p’ will start glowing. For example, if we turn ON the switch labelled 2, then all the bulbs having numbers 2, 4, 6, 8, 10, ... i.e all bulbs with numbers as multiples of 2 will start glowing.

You are given an array/list ‘LABELS’ consisting of ‘N’ unique prime integers representing the label of the switches and an integer ‘K’. Your task is to find the integer assigned to Kth glowing bulb from the start when all these ‘N’ switches are turned ON.

Note :
1. Some bulbs can glow by multiple switches and some are not glowed by any switch.
2. If any of the switches that can glow a bulb is turned ‘ON’, then the corresponding bulb will glow.
Example :
Consider 3 switches with labels [3, 5,  7] and we need to find the 5th glowing bulb from the start after turning these 3 switches ON.
We can see that bulbs numbered  3, 6, 9, 15, 18 …  will glow if the switch having label 3 is turned ON.
The bulbs numbered 5, 10, 15, 20 … will glow if the switch having label 5 is turned ON.
The bulbs numbered 7, 14, 21, 28 … will glow if the switch having label 7 is turned ON.
It implies that bulbs numbered 3, 5, 6, 7, 9, 10, 14, 15, 18, 20, 21… will glow when these three switches are turned ON.
The 5th glowing bulb from start is assigned integer 9. Thus, we should return 9.
Input Format :
The first line of input contains an integer ‘T’ denoting the number of test cases. Then ‘T’ test cases follow.

The first line of each test case consists of two space-separated integers ‘N’ and ‘K’ respectively.

The second line of each test case consists of ‘N’ space-separated prime integers representing array/list ‘LABELS’.
Output Format :
For each test case, print the integer assigned to the Kth glowing bulb when all the given switches in ‘LABELS’ are turned ON.

Print the output of each test case in a separate line.

Note :

You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 50
1 <= N <= 10
1 <= K <= 10^12
1 < LABELS[i] < 30

Where 'LABELS[i]' is a prime integer and all integers in array/list ‘LABELS’ are distinct.

Time limit: 1 sec

Approaches

01 Approach

This approach is straightforward, we will keep a ‘COUNTER’ initialized with 0. Then we iterate over integers starting from 2. For each integer, we will check whether it is multiple of any of the given ‘N’ prime numbers in the list ‘LABELS’ or not.  If it is multiple of any of those prime numbers then we increment the ‘COUNTER’ by ‘1’.  When the value of ‘COUNTER’ becomes ‘K’ then we return the current integer as it will be the integer assigned to the Kth glowing bulb.

 

Algorithm :

  • Initialize an integer variable ‘COUNTER’ := 0.
  • Run a loop where ‘i’ starts 1 and in each iteration do the following -:
    • Iterate over the list LABELS, and if for any prime integer ‘p’ in the list LABELS if  i % p = 0, then do COUNTER := COUNTER + 1 and break this inner loop.
    • If COUNTER = ‘K’ then return ‘i’.

02 Approach

We can say, that the integer assigned to the Kth glowing bulb is in the range [2, K * 30]  because LABELS[i] can not be greater than 30 (see constraints), and so this integer cannot be greater than K * LABELS[i] for any valid ‘i’.

Now, by using the inclusion-exclusion principle we can find the number of bulbs having assigned integer at most ‘X’  that will glow when all the given switches are turned ON.

Let this number of bulbs be COUNT, then if there are only two prime numbers p1, p2, in the list LABELS:

Then COUNT  = X/p1 + X/p2 - X/(p1*p2) where, ‘/’ denote floor division.

Similarly for three primes p1, p2, p3 -:

COUNT = X/p1 + X/p2 + X/p3 - X/(p1*p2) - X/(p1*p3) - X/(p2*p3) + X(p1*p2*p3)

We can generalize it for ‘N’ primes in the same way.

We can say, that if for any integer ‘X’ the number of bulbs having assigned integer at most ‘X’  that will glow when all the given switches are turned ON is less than ‘K’  then our answer must be greater than ‘X’ otherwise it will be less than or equal to ‘K’.  This suggests we use binary search in this problem.  We can solve it using binary search as follows.

 

Algorithm

  • Initialize two integer variables ‘START’:= 2 and ‘END’:= K*30.
  • Run a while loop till ‘END’ > ‘START’ and in each iteration do the following -:
    • Initialize ‘MID’:= ‘START’ + (‘END’ - ‘START’) / 2.
    • Initialize an integer variable ‘COUNT’:= 0.
    • Now we will count the number of bulbs that have assigned integer at most ‘MID’  that will glow when all the given switches are turned ON using the inclusion-exclusion principle. To do this we run a loop where ‘i’ ranges from 1 to 2 ^ N - 1 and in each iteration we do following -:
      • Initialize integer variable ‘PRODUCT’:= 1 and boolean variable ‘SIGN’:= true
      • Run a loop where ‘j’ ranges from 0 to ‘N’ and if ‘j’th bit is set in ‘i’ then do ‘PRODUCT’:= ‘PRODUCT’ * LABELS[j] and ‘SIGN’:= !SIGN 
      • If ‘SIGN’ = true, then do ‘COUNT’ -= MID/PRODUCT otherwise do COUNT += MID / PRODUCT.
    • If ‘COUNT’>= ‘K’,  then do ‘END’= ‘MID’.
    • Otherwise do ‘START’= ‘MID’ + 1.
  • Return ‘START’.