Today in an exam I faced a problem which is Toggled Switches(Hacker Earth) as it is an exam question So, I am unable to send the question or its link (exactly).
Problem: we have to print a number representing the number of times do bulb changed its state. The bulb is attached to the "n" number of switches. So the bulb will be in the "on" state only when half or more than half of the given n of switches to be "on".
1 represents on state
0 for off respectively.
There is also a query that will be given to represent which switch has to be toggled ( change from on to off or vice-versa 1 ->0 or 0->1). So, by toggling may or may not the bulb state change. so we have to return the number of times it changed its state.
input:
1 - number of test case
3 - number of switches (n)
1 1 0 - initial all n switches states
3 - number of switches to be toggled
3 2 1 -> (3 represents third switch i.e as index wise second in switches array.) switches that have to be toggled.
output:
1 - number of times the bulb changed its state .
explanation: initial array or switches state [1, 1, 0] as half or more than half switches are in on. hence bulb is in on state. as per queries [3,2,1]
we have to toggle the 3 rd switch. As initially third is in off [1,1,0] after toggling it changes to on [1,1,1] present switches state. as all are in on position bulb is on. ( no changes in the state of the bulb)
Now, have to toggle 2 nd switch so [1,1,1] changes to [1,0,1] switches states as half switches are on the bulb is on. ( no changes in the state of the bulb)
Now, 1 st switch to be toggled. Hence [1,0,1] changes to [0,0,1]. as not equal or more than half switches are in on states hence bulb is going to be off. ( now bulb state is changed)
Hence the output is one.
I wrote the answer to this I am only able to solve the sample test case. No hidden test case is got the right answer and some got time exceeds error. so totally 0 marks.
I am a beginner so please explain vividly where I am wrong what is wrong? how it can be efficient ?.
and my code is
import java.util.*;
public class Main{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int test_cases = scan.nextInt();
while(test_cases>0){
//number of switches
int n = scan.nextInt();
// contains on / off positions of switches
int [] switches = new int[n];
for(int i =0;i<n;i++)
switches[i]=scan.nextInt();
//number of queries
int q = scan.nextInt();
// starts at 1 i.e queries[0]=1 but represents switch index 1(queries representing switch position and tha position starts from 1)
int [] queries = new int[q];
for(int i =0;i<q;i++)
queries[i]=scan.nextInt();
int result = toggled(n,switches,q,queries);
System.out.println(result);
test_cases--;
}
}
static int toggled(int n,int switches[],int q,int queries[]){
if(n==1)
return q;
int result =0;
//giving switch state on or off
boolean initial = on(switches);
//System.out.println("initial is "+initial);
//to represent on or off for all toggle including initial
boolean [] states = new boolean[q+1];
states[0] = initial;
int count = 0;
//to represent index of states
int state_count =1;
while(count<q){
if((switches[queries[count]-1] & 1 )== 1)
switches[queries[count]-1] = 0;
else
switches[queries[count]-1] = 1;
states[state_count++]=on(switches);
count++;
}
//System.out.println("state_count is "+state_count);
for(int i =1;i<state_count;i++){
if(initial ^ states[i] )
{
result+=1;
initial = states[i];
}
}
return result;
}
//method to evaluate whether to consider on or off ( to on(bulb) have to on atleast half of the switches)
static boolean on(int arr[]){
int sum = 0;
for(int i =0;i<arr.length;i++)
sum+=arr[i];
if(sum>=arr.length/2.0)
return true;
else
return false;
}
}