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
|
207 Discussions
|
Please Login in order to post a comment
**long lights(int n) { long ways = 1; for(int i = 0 ; i < n ; i++){ ways *= 2; if(ways > 100000) ways %= 100000; } return ways-1;
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) {