Count the number of times a Bulb switches its state
Last Updated :
19 Oct, 2021
Given two arrays switch[], consisting of binary integers denoting whether a switch is ON(0) or OFF(1), and query[], where query[i] denotes the switch to be toggled. The task after completing all the switch toggles is to print the number of times the bulb changes its state, i.e. from ON to OFF or vice-versa.
Examples :
Input: switch[] ={1, 1, 0}, query[] = {3, 2, 1}
Output : 1
Explanation:
Initial state of switches {1, 1, 0}. Since the count of 1’s = 2 (>= ceil(N / 2)), the bulb glows.
query[0] = 3
Next state of switches {1, 1, 1}. Since the count of 1’s = 3 (>= ceil(N / 2)), the bulb glows.
query[1] = 2
Next state of switches {1, 0, 1}. Since the count of 1’s = 2 (>= ceil(N / 2)), the bulb glows.
query[2] = 1
Next state of switches {0, 0, 1}.. Since the count of 1’s = 1 (< ceil(N / 2)), the bulb turns off.
Therefore, the bulb witches from glowing to non-glowing state only once.
Input : switch[] = { 1, 1, 0, 0, 1, 1 }
query[] = { 4, 3, 6 }
Output: 0
Approach : Follow the steps below to solve the problem:
- Traverse the array arr[].
- Count the number of 1s to keep track of the initial state of the bulb.
- Traverse the array query[].
- For every query[i], update arr[] and the count of 1s. Check for the current state of the bulb accordingly.
- If the previous and the current states are found to be different, then increment count.
- Finally, print the value of count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int A[], int n,
int Q[], int q)
{
int one = 0;
for ( int i = 0; i < n; i++)
if (A[i] == 1)
one++;
int glows = 0, count = 0;
if (one >= ceil (n / 2))
glows = 1;
for ( int i = 0; i < q; i++) {
int prev = glows;
if (A[Q[i] - 1] == 1)
one--;
if (A[Q[i] - 1] == 0)
one++;
A[Q[i] - 1] ^= 1;
if (one >= ceil (n / 2.0)) {
glows = 1;
}
else {
glows = 0;
}
if (prev != glows)
count++;
}
return count;
}
int main()
{
int n = 3;
int arr[] = { 1, 1, 0 };
int q = 3;
int Q[] = { 3, 2, 1 };
cout << solve(arr, n, Q, q);
return 0;
}
|
Java
import java.util.*;
public class Main {
static int solve( int [] A, int n,
int Q[], int q)
{
int one = 0 ;
for ( int i = 0 ; i < n; i++)
if (A[i] == 1 )
one++;
int glows = 0 , count = 0 ;
if (one >= ( int )Math.ceil(n / 2 ))
glows = 1 ;
for ( int i = 0 ; i < q; i++) {
int prev = glows;
if (A[Q[i] - 1 ] == 1 )
one--;
if (A[Q[i] - 1 ] == 0 )
one++;
A[Q[i] - 1 ] ^= 1 ;
if (one >= ( int )Math.ceil(n / 2.0 )) {
glows = 1 ;
}
else {
glows = 0 ;
}
if (prev != glows)
count++;
}
return count;
}
public static void main(String args[])
{
int n = 3 ;
int arr[] = { 1 , 1 , 0 };
int q = 3 ;
int Q[] = { 3 , 2 , 1 };
System.out.println(
solve(arr, n, Q, q));
}
}
|
Python3
import math
def solve(A, n, Q, q):
one = 0
for i in range ( 0 , n):
if (A[i] = = 1 ):
one + = 1
glows = 0
count = 0
if (one > = int (math.ceil(n / 2 ))):
glows = 1
for i in range ( 0 , q):
prev = glows
if (A[Q[i] - 1 ] = = 1 ):
one - = 1
if (A[Q[i] - 1 ] = = 0 ):
one + = 1
A[Q[i] - 1 ] ^ = 1
if (one > = int (math.ceil(n / 2.0 ))):
glows = 1
else :
glows = 0
if (prev ! = glows):
count + = 1
return count
n = 3
arr = [ 1 , 1 , 0 ]
q = 3
Q = [ 3 , 2 , 1 ]
print (solve(arr, n, Q, q))
|
C#
using System;
class GFG
{
static int solve( int [] A, int n,
int [] Q, int q)
{
int one = 0;
for ( int i = 0; i < n; i++)
if (A[i] == 1)
one++;
int glows = 0, count = 0;
if (one >= ( int )Math.Ceiling(( double )n / 2))
glows = 1;
for ( int i = 0; i < q; i++) {
int prev = glows;
if (A[Q[i] - 1] == 1)
one--;
if (A[Q[i] - 1] == 0)
one++;
A[Q[i] - 1] ^= 1;
if (one >= ( int )Math.Ceiling(( double )n / 2.0)) {
glows = 1;
}
else {
glows = 0;
}
if (prev != glows)
count++;
}
return count;
}
static public void Main ()
{
int n = 3;
int [] arr = { 1, 1, 0 };
int q = 3;
int [] Q = { 3, 2, 1 };
Console.WriteLine(
solve(arr, n, Q, q));
}
}
|
Javascript
<script>
function solve(A, n, Q, q)
{
var one = 0;
for ( var i = 0; i < n; i++)
if (A[i] == 1)
one++;
var glows = 0, count = 0;
if (one >= Math.ceil(n / 2))
glows = 1;
for ( var i = 0; i < q; i++) {
var prev = glows;
if (A[Q[i] - 1] == 1)
one--;
if (A[Q[i] - 1] == 0)
one++;
A[Q[i] - 1] ^= 1;
if (one >= Math.ceil(n / 2.0)) {
glows = 1;
}
else {
glows = 0;
}
if (prev != glows)
count++;
}
return count;
}
var n = 3;
var arr = [1, 1, 0];
var q = 3;
var Q = [3, 2, 1];
document.write( solve(arr, n, Q, q));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...