We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Mathematics
- Fundamentals
- Diwali Lights
- Discussions
Diwali Lights
Diwali Lights
Sort by
recency
|
206 Discussions
|
Please Login in order to post a comment
The problem here is big integers We would not use big integers if we apply modular ariphmetics (A*B) mod M = (A mod M * B mod M) mod M
https://www.geeksforgeeks.org/modular-multiplication/ https://www.khanacademy.org/computing/computer-science/cryptography/modarithmetic/a/modular-multiplication
my solution:
https://github.com/Tusenka/hackerrank/blob/main/bulbs.py
Python Solution: def lights(n): # Write your code here if n==1: return 1 else: return ((2**n-1)%10**5)
Draw out the truth table for N inputs and you will see that there is one input where all the values are 0. The rest of the inputs has at least one set bit. that means the answer is (2 ^ N) - 1 and then take the modulus of it.
Here's my java solution:
public static long lights(int n) {
it's like sum of combinations.. nC0 + nC1 + nC2 ... +nCk +..nCn = 2^n;
where "k" is the number of lights at a moment of time. we'll traverse from [0 , n-1] and creating 2^n slowly with limit 1e5
at last we won't consider the case where no light bulb is ON.
2^n - nC0 -> 2^n - 1