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
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 o...read more
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’.
O(1).
We are not using any extra space here.
Time Complexity: OtherExplanation:O(N * (2 ^ N) * log(K)), where N’ denotes the size of the array/list ‘LABELS’ and ‘K’ is the given integer.
We will have log(K) iteration in binary search and in each iteration it takes N * 2 ^ N time to count the number of bulbs less than ‘MID’ that will glow. Thus the overall complexity will be O(N * (2 ^ N) * log(K)).
Java (SE 1.8)
/*
Time Complexity : O(N * (2 ^ N) * log(K))
Space Complexity : O(1)
Where 'N' denotes the size of the array/list 'LABELS' and 'K' is the given integer.
*/
import java.util.ArrayList;
public class Solution
{
public static long findKthGlowingBulb(ArrayList labels, long k)
{
// Number of switches.
int n = labels.size();
long start = 0, end = k*30;
// Binary search to find answer.
while (end > start)
{
long mid = start + (end - start) / 2;
long count = 0;
// Using inclsion-exclusion principle to count number of glowing bulbs having
// assigned integer not more than 'mid'.
for (int i = 1; i < (1 << n); i++)
{
long product = 1;
boolean sign = true;
for (int j = 0; j < n; j++)
{
if ((i & (1 << j)) > 0)
{
product = product * labels.get(j);
sign = !sign;
}
}
if (sign == true)
{
count -= mid / product;
}
else
{
count += mid / product;
}
}
// Answer will be in range [start, mid].
if (count >= k)
{
end = mid;
}
// Answer will be in range [mid+1, end].
else
{
start = mid + 1;
}
}
return start;
}
}
C++ (g++ 5.4)
/*
Time Complexity : O(N * (2 ^ N) * log(K))
Space Complexity : O(1)
Where ‘N’ denotes the size of the array/list ‘LABELS’ and ‘K’ is the given integer.
*/
long long findKthGlowingBulb(vector &labels, long long k) {
// Number of switches.
int n = labels.size();
long long start = 0, end = k*30;
// Binary search to find answer.
while (end > start) {
long long mid = start + (end - start) / 2;
long long count = 0;
// Using inclsion-exclusion principle to count number of glowing bulbs having
// assigned integer not more than 'mid'.
for (int i = 1; i < (1 << n); i++) {
long long product = 1;
bool sign = true;
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
product = product * labels[j];
sign = !sign;
}
}
if (sign == true) {
count -= mid / product;
}
else {
count += mid / product;
}
}
// Answer will be in range [start, mid].
if (count >= k) {
end = mid;
}
// Answer will be in range [mid+1, end].
else {
start = mid + 1;
}
}
return start;
}
Python (3.5)
'''
Time Complexity : O(N * (2 ^ N) * log(K))
Space Complexity : O(1)
Where ‘N’ denotes the size of the array/list ‘LABELS’ and ‘K’ is the given integer.
'''
def findKthGlowingBulb(labels, k):
# Number of switches.
n = len(labels)
start, end = 0, k * 30
# Binary search to find answer.
while (end > start):
mid = start + (end - start) // 2
count = 0
# Using inclsion-exclusion principle to count number of glowing bulbs having
# assigned integer not more than 'mid'.
for i in range(1, 1 << n):
product = 1
sign = True
for j in range(n):
if (i & (1 << j)):
product = product * labels[j]
sign = not sign
if (sign == True):
count -= mid // product
else:
count += mid // product
# Answer will be in range [start, mid].
if (count >= k):
end = mid
# Answer will be in range [mid+1, end].
else:
start = mid + 1
return start
Top Infosys System Engineer Specialist interview questions & answers
Top HR questions asked in Infosys System Engineer Specialist
Reviews
Interviews
Salaries
Users/Month