Sunday, February 21, 2021

Published February 21, 2021 by Anonymous with 0 comment

Count the number of times a Bulb switches its state

  

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));

    }

}

Let's block ads! (Why?)

      edit

0 comments:

Post a Comment