Skip to content
Related Articles

Related Articles

Count the number of times a Bulb switches its state
  • Difficulty Level : Medium
  • Last Updated : 02 Mar, 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:



  1. Traverse the array arr[].
  2. Count the number of 1s to keep track of the initial state of the bulb.
  3. Traverse the array query[].
  4. For every query[i], update arr[] and the count of 1s. Check for the current state of the bulb accordingly.
  5. If the previous and the current states are found to be different, then increment count.
  6. Finally, print the value of count.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number of
// times a bulb switches its state
int solve(int A[], int n,
          int Q[], int q)
{
   
  // Count of 1s
  int one = 0;
 
  // Traverse the array
  for (int i = 0; i < n; i++)
 
    // Update count of 1s
    if (A[i] == 1)
      one++;
 
  // Update the status of bulb
  int glows = 0, count = 0;
 
  if (one >= ceil(n / 2))
    glows = 1;
 
  // Traverse the array Q[]
  for (int i = 0; i < q; i++) {
 
    // Stores previous state
    // of the bulb
    int prev = glows;
 
    // Toggle the switch and
    // update count of 1s
    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 the bulb switches state
    if (prev != glows)
      count++;
  }
 
  // Return count
  return count;
}
 
// Driver Code
int main()
{
   
  // Input
  int n = 3;
  int arr[] = { 1, 1, 0 };
  int q = 3;
 
  // Queries
  int Q[] = { 3, 2, 1 };
 
  // Function call to find number
  // of times the bulb toggles
  cout << solve(arr, n, Q, q);
 
  return 0;
}
 
// This code is contributed by splevel62.

Java




// Java implementation of
// the above approach
 
import java.util.*;
public class Main {
 
    // Function to find the number of
    // times a bulb switches its state
    static int solve(int[] A, int n,
                     int Q[], int q)
    {
        // Count of 1s
        int one = 0;
 
        // Traverse the array
        for (int i = 0; i < n; i++)
 
            // Update count of 1s
            if (A[i] == 1)
                one++;
 
        // Update the status of bulb
        int glows = 0, count = 0;
 
        if (one >= (int)Math.ceil(n / 2))
            glows = 1;
 
        // Traverse the array Q[]
        for (int i = 0; i < q; i++) {
 
            // Stores previous state
            // of the bulb
            int prev = glows;
 
            // Toggle the switch and
            // update count of 1s
            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 the bulb switches state
            if (prev != glows)
                count++;
        }
 
        // Return count
        return count;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Input
        int n = 3;
        int arr[] = { 1, 1, 0 };
        int q = 3;
 
        // Queries
        int Q[] = { 3, 2, 1 };
 
        // Function call to find number
        // of times the bulb toggles
        System.out.println(
            solve(arr, n, Q, q));
    }
}

Python3




# Python program for
# the above approach
import math
 
# Function to find the number of
# times a bulb switches its state
def solve(A, n, Q, q):
   
    # count of 1's
    one = 0
     
    # Traverse the array
    for i in range(0, n):
       
        # update the array
        if (A[i] == 1):
            one += 1
             
    # update the status of bulb
    glows = 0
    count = 0
    if (one >= int(math.ceil(n / 2))):
        glows = 1
 
    # Traverse the array Q[]
    for i in range(0, q):
       
        # stores previous state of
        # the bulb
        prev = glows
 
        # Toggle the switch and
        # update the count of 1's
        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 the bulb switches state
        if (prev != glows):
            count += 1
         
    # Retuen count
    return count
 
# Driver code
 
# Input
n = 3
arr = [1, 1, 0]
q = 3
 
# Queries
Q = [3, 2, 1]
 
# Function call to find number
# of times the bulb toggles
print(solve(arr, n, Q, q))
 
# This code id contributed by Virusbuddah

C#




// C# program for the above approach
using System;
class GFG
{
 
  // Function to find the number of
  // times a bulb switches its state
  static int solve(int[] A, int n,
                   int[] Q, int q)
  {
 
    // Count of 1s
    int one = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++)
 
      // Update count of 1s
      if (A[i] == 1)
        one++;
 
    // Update the status of bulb
    int glows = 0, count = 0;
 
    if (one >= (int)Math.Ceiling((double)n / 2))
      glows = 1;
 
    // Traverse the array Q[]
    for (int i = 0; i < q; i++) {
 
      // Stores previous state
      // of the bulb
      int prev = glows;
 
      // Toggle the switch and
      // update count of 1s
      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 the bulb switches state
      if (prev != glows)
        count++;
    }
 
    // Return count
    return count;
  }
 
  // Driver Code
  static public void Main ()
  {
 
    // Input
    int n = 3;
    int[] arr = { 1, 1, 0 };
    int q = 3;
 
    // Queries
    int[] Q = { 3, 2, 1 };
 
    // Function call to find number
    // of times the bulb toggles
    Console.WriteLine(
      solve(arr, n, Q, q));
  }
}
 
// This code is contributed by susmitakundugoaldanga.

 
 

Output: 
1

 

Time Complexity: O(N)
Auxiliary Space: O(1) 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :