

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.
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.
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’.
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.
You do not need to print anything, it has already been taken care of. Just implement the given function.
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
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.
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 -:
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.
First Digit One
Special Digit Numbers
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store