Fair attraction In olden days, one could encounter the following attraction at a
ID: 3864075 • Letter: F
Question
Fair attraction In olden days, one could encounter the following attraction
at a fair. A light bulb was connected to several switches in such a way that it
lighted up only when all the switches were closed. Each switch was controlled
by a push button; pressing the button toggled the switch, but there was no
way to know the state of the switch. The object was to turn the light bulb on.
Design an algorithm to turn on the light bulb with the minimum number of
button pushes needed in the worst case for n switches.
Explanation / Answer
Let us consider an array of switches of size n
Let us denote it as switch[n]
also consider if switch[i] is 1 then it is open
if switch[i] is 0 then it is close
Algorithm :-
input : An array switch with values of 0's and 1's in random
for i = 1 to n
if (switch[i] == 1) switch[i]=0; // this step is known as toggle
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.