On the eve of Diwali, Hari is decorating his house with a serial light bulb set. The serial light bulb set has N bulbs placed sequentially on a string which is programmed to change patterns every second. If at least one bulb in the set is on at any given instant of time, how many different patterns of light can the serial light bulb set produce?
Note: Lighting two bulbs *-* is different from **-
Example:
Input: n = 4
Output: 15
Approach
Java
public class DiwaliLights {public static void main(String[] args) {int n = 4;System.out.println(lights(n));}static long mod = 100000;static long power(long a, long n) {// Base Caseif (n == 0)return 1;long res = power(a, n / 2) % mod;if (n % 2 == 1)return (res * res * a) % mod;elsereturn (res * res) % mod;}static long lights(long n) {return (power(2, n) % mod - 1);}}
C++
#include <bits/stdc++.h>using namespace std;#define mod 100000long power(long a, long n){//Base Caseif (n == 0)return 1;long res = power(a, n / 2) % mod;if (n & 1)return (res * res * a) % mod;elsereturn (res * res) % mod;}long lights(long n){return (power(2, n) % mod - 1);}int main(){int n = 4;cout << lights(n) << "\n";return 0;}
Read Interview Questions
Exception Handling Interview Questions
DBMS Interview Questions Set -1
DBMS Interview Questions Set -2
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment