- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bulb Switcher II in C++
Suppose there is a room with n lights which are switched on initially and 4 buttons present on the wall. After performing exactly m unknown operations towards buttons, we need to return how many different kinds of status of the n lights could be. So consider n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are as follows −
- Flip all the lights.
- Flip lights with even numbers.
- Flip lights with odd numbers.
- Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...
Now if n = 3 and m = 1, then there will be 4 operations, these are, [off, on, off], [on, off, on], [off, off, off], [off, on, on]
To solve this, we will follow these steps −
- if n is 0 or m is 0, then return 1
- if n is 1, then return 2
- if n is 2, then return 3, when m is 1, otherwise return 4
- if m is 1, then return 4
- if m is 2, then return 7, otherwise return 8.
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int flipLights(int n, int m) { if (m == 0 || n == 0) return 1; if (n == 1) return 2; if (n == 2) return m == 1? 3:4; if (m == 1) return 4; return m == 2? 7:8; } }; main(){ Solution ob; cout << (ob.flipLights(3, 1)); }
Input
3 1
Output
4
- Related Articles
- Bulb Switcher in C++
- Bulb Switcher III in C++
- Program to make a bulb switcher using binary string using Python
- A bulb lights up when connected to a battery. State the energy change which takes place: (i) in the battery. (ii) in the bulb.
- (i) A lot of 20 bulbs contain 4 defective ones. One bulb is drawn at random from the lot. What is the probability that this bulb is defective?(ii) Suppose the bulb drawn in (i) is not defective and not replaced. Now bulb is drawn at random from the rest. What is the probability that this bulb is not defective?
- The apparatus given in the adjoining figure was set up to demonstrate electrical conductivity.Which among the following statement (s) is (are) correct?(i) Bulb will not glow because the electrolyte is not acidic.(ii) Bulb will glow because HCl is a strong acid and furnishes ions for conduction.(iii) Bulb will not glow because the circuit is incomplete(iv) Bulb will not glow because it depends upon the type of electrolytic solution(a) (i) and (iii)(b) (ii) and (iv)(c) (ii) only(d)(iv) only"
- In an attempt to demonstrate electrical conductivity through an electrolyte, the following apparatus (Figure 2.1) was set up. Which among the following statement(s) is(are) correct?(i) Bulb will not glow because the electrolyte is not acidic(ii) Bulb will glow because NaOH is a strong base and furnishes ions for conduction.(iii) Bulb will not glow because the circuit is incomplete(iv) Bulb will not glow because it depends upon the type of electrolytic solution(a) (i) and (iii)(b) (ii) and (iv)(c) (ii) only(d) (iv) only"
- What invented light bulb?
- Permutations II in C++
- Subsets II in C++
- Figure A and B, show a bulb connected to a cell in two different ways.$(i)$. What will be the direction of the current through the bulb in both the cases $(Q to P or P to Q)$?$(ii)$. Will the bulb glow in both cases?$(iii)$. Does the brightness of the glowing bulb depend on the direction of current through it?"
- Three bulbs A, B, and C are connected in a circuit as shown in the figure. When the switch is ‘ON’$(a)$. bulb C will glow first.$(b)$. bulb B and C will glow simultaneously and bulb A will glow after some time.$(c)$. all the bulbs A
- Draw the symbols of the following circuit components.$(i)$. electric cell$(ii)$. switch in off position$(iii)$. electric bulb$(iv)$. battery
- Switching on and off bulb in JavaScript
- In the circuit shown in the figure, when the switch is moved to the ’ON’ position,$(a)$. the bulb A will glow first$(b)$. the bulb B will glow first$(c)$. the bulb C will glow first$(d)$. all bulbs will glow together."

Advertisements