Active and Inactive cells after k Days - GeeksforGeeks

07 Apr, 2021
Given a binary array of size n where n > 3. A true (or 1) value in the array means active and false (or 0) means inactive. Given a number k, the task is to find count of active and inactive cells after k days. After every day, status of i’th cell becomes active if left and right cells are not same and inactive if left and right cell are same (both 0 or both 1). Since there are no cells before leftmost and after rightmost cells, the value cells before leftmost and after rightmost cells is always considered as 0 (or inactive). 
Examples:  
Input  : cells[] = {1, 0, 1, 1}, k = 2
Output : Active cells = 3, Inactive cells = 1
After 1 day,  cells[] = {0, 0, 1, 1}
After 2 days, cells[] = {0, 1, 1, 1}

Input : cells[] = {0, 1, 0, 1, 0, 1, 0, 1},  k = 3
Output: Active Cells = 2 , Inactive Cells = 6
Explanation : 
After 1 day, cells[] = {1, 0, 0, 0, 0, 0, 0, 0}
After 2 days, cells[] = {0, 1, 0, 0, 0, 0, 0, 0}
After 3 days, cells[] =  {1, 0, 1, 0, 0, 0, 0, 0}

Input : cells[] = {0, 1, 1, 1, 0, 1, 1, 0},  k = 4
Output: Active Cells = 3 , Inactive Cells = 5
 
The only important thing is to make sure that we maintain a copy of given array because we need previous values to update for next day. Below are detailed steps.  
First we copy the cells[] array into temp[] array and make changes in temp[] array according to given condition.In the condition, it is given that if immediate left and right cell of i’th cell either inactive or active the next day i becomes inactive i.e; (cells[i-1] == 0 and cells[i+1] == 0) or (cells[i-1] == 1 and cells[i+1] == 1) then cells[i] = 0, these conditions can be applied using XOR of cells[i-1] and cells[i+1].For 0’th index cell temp[0] = 0^cells[1] and for (n-1)’th index cell temp[n-1] = 0^cells[n-2].Now for index 1 to n-2, do the following operation temp[i] = cells[i-1] ^ cells[i+1]Repeat the process till k days are completed.
First we copy the cells[] array into temp[] array and make changes in temp[] array according to given condition.
In the condition, it is given that if immediate left and right cell of i’th cell either inactive or active the next day i becomes inactive i.e; (cells[i-1] == 0 and cells[i+1] == 0) or (cells[i-1] == 1 and cells[i+1] == 1) then cells[i] = 0, these conditions can be applied using XOR of cells[i-1] and cells[i+1].
For 0’th index cell temp[0] = 0^cells[1] and for (n-1)’th index cell temp[n-1] = 0^cells[n-2].
Now for index 1 to n-2, do the following operation temp[i] = cells[i-1] ^ cells[i+1]
Repeat the process till k days are completed.
Following is the implementation of above steps.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count active and inactive cells after k// days#include<bits/stdc++.h>using namespace std; // cells[] - store current status of cells// n - Number of cells// temp[] - to perform intermediate operations// k - number of days// active - count of active cells after k days// inactive - count of active cells after k daysvoid activeAndInactive(bool cells[], int n, int k){    // copy cells[] array into temp [] array    bool temp[n];    for (int i=0; i<n ; i++)        temp[i] = cells[i];     // Iterate for k days    while (k--)    {        // Finding next values for corner cells        temp[0]   = 0^cells[1];        temp[n-1] = 0^cells[n-2];         // Compute values of intermediate cells        // If both cells active or inactive, then temp[i]=0        // else temp[i] = 1.        for (int i=1; i<=n-2; i++)            temp[i] = cells[i-1] ^ cells[i+1];         // Copy temp[] to cells[] for next iteration        for (int i=0; i<n; i++)            cells[i] = temp[i];    }     // count active and inactive cells    int active = 0, inactive = 0;    for (int i=0; i<n; i++)        (cells[i] == 1)? active++ : inactive++;     printf("Active Cells = %d, Inactive Cells = %d",           active, inactive);} // Driver program to check the test caseint main(){    bool cells[] = {0, 1, 0, 1, 0, 1, 0, 1};    int k = 3;    int n =  sizeof(cells)/sizeof(cells[0]);    activeAndInactive(cells, n, k);    return 0;}
// Java program to count active and// inactive cells after k days class GFG {     // cells[] - store current status// of cells n - Number of cells// temp[] - to perform intermediate operations// k - number of days// active - count of active cells after k days// inactive - count of active cells after k daysstatic void activeAndInactive(boolean cells[],                                 int n, int k){    // copy cells[] array into temp [] array    boolean temp[] = new boolean[n];    for (int i = 0; i < n; i++)    temp[i] = cells[i];     // Iterate for k days    while (k-- > 0) {             // Finding next values for corner cells    temp[0] = false ^ cells[1];    temp[n - 1] = false ^ cells[n - 2];     // Compute values of intermediate cells    // If both cells active or inactive, then    // temp[i]=0 else temp[i] = 1.    for (int i = 1; i <= n - 2; i++)        temp[i] = cells[i - 1] ^ cells[i + 1];     // Copy temp[] to cells[] for next iteration    for (int i = 0; i < n; i++)        cells[i] = temp[i];    }     // count active and inactive cells    int active = 0, inactive = 0;    for (int i = 0; i < n; i++)    if (cells[i] == true)        active++;    else        inactive++;     System.out.print("Active Cells = " + active + ", " +                     "Inactive Cells = " + inactive);} // Driver codepublic static void main(String[] args){    boolean cells[] = {false, true, false, true,                       false, true, false, true};    int k = 3;    int n = cells.length;    activeAndInactive(cells, n, k);}} // This code is contributed by Anant Agarwal.
# Python program to count# active and inactive cells after k# days # cells[] - store current# status of cells# n - Number of cells# temp[] - to perform# intermediate operations# k - number of days# active - count of active# cells after k days# inactive - count of active# cells after k daysdef activeAndInactive(cells,n,k):         # copy cells[] array into temp [] array    temp=[]    for i in range(n+1):        temp.append(False)    for i in range(n):        temp[i] = cells[i]      # Iterate for k days    while (k >0):             # Finding next values for corner cells        temp[0]   = False^cells[1]        temp[n-1] = False^cells[n-2]          # Compute values of intermediate cells        # If both cells active or        # inactive, then temp[i]=0        # else temp[i] = 1.        for i in range(1,n-2+1):            temp[i] = cells[i-1] ^ cells[i+1]          # Copy temp[] to cells[]        # for next iteration        for i in range(n):            cells[i] = temp[i]        k-=1         # count active and inactive cells    active = 0    inactive = 0;    for i in range(n):        if(cells[i] == True):            active+=1        else:            inactive+=1      print("Active Cells =",active," , "          , "Inactive Cells =",           inactive) # Driver code cells = [False, True, False, True,         False, True, False, True]k = 3n =len(cells) activeAndInactive(cells, n, k) # This code is contributed# by Anant Agarwal.
// C# program to count active and// inactive cells after k daysusing System; class GFG {     // cells[] - store current status// of cells n - Number of cells// temp[] - to perform intermediate// operations k - number of days// active - count of active cells// after k days inactive - count// of active cells after k daysstatic void activeAndInactive(bool []cells,                              int n, int k){         // copy cells[] array into    // temp [] array    bool []temp = new bool[n];    for (int i = 0; i < n; i++)    temp[i] = cells[i];     // Iterate for k days    while (k-- > 0) {             // Finding next values    // for corner cells    temp[0] = false ^ cells[1];    temp[n - 1] = false ^ cells[n - 2];     // Compute values of intermediate cells    // If both cells active or inactive, then    // temp[i]=0 else temp[i] = 1.    for (int i = 1; i <= n - 2; i++)        temp[i] = cells[i - 1] ^ cells[i + 1];     // Copy temp[] to cells[]    // for next iteration    for (int i = 0; i < n; i++)        cells[i] = temp[i];    }     // count active and inactive cells    int active = 0, inactive = 0;    for (int i = 0; i < n; i++)    if (cells[i] == true)        active++;    else        inactive++;     Console.Write("Active Cells = " + active + ", " +                  "Inactive Cells = " + inactive);} // Driver codepublic static void Main(){    bool []cells = {false, true, false, true,                    false, true, false, true};    int k = 3;    int n = cells.Length;    activeAndInactive(cells, n, k);}} // This code is contributed by Nitin Mittal.
<?php// PHP program to count active// and inactive cells after k// days // cells[] - store current status// of cells n - Number of cells// temp[] - to perform intermediate// operations k - number of days// active - count of active cells// after k days inactive - count of// active cells after k daysfunction activeAndInactive($cells, $n, $k){         // copy cells[] array into    // temp [] array    $temp = array();    for ($i = 0; $i < $n ; $i++)        $temp[$i] = $cells[$i];     // Iterate for k days    while ($k--)    {                 // Finding next values        // for corner cells        $temp[0] = 0 ^ $cells[1];        $temp[$n - 1] = 0 ^ $cells[$n - 2];         // Compute values of        // intermediate cells        // If both cells active        // or inactive, then temp[i]=0        // else temp[i] = 1.        for ($i = 1; $i <= $n - 2; $i++)            $temp[$i] = $cells[$i - 1] ^                        $cells[$i + 1];         // Copy temp[] to cells[]        // for next iteration        for ($i = 0; $i < $n; $i++)            $cells[$i] = $temp[$i];    }     // count active and    // inactive cells    $active = 0;$inactive = 0;    for ($i = 0; $i < $n; $i++)        ($cells[$i] == 1)? $active++ : $inactive++;     echo "Active Cells = ", $active, " ,Inactive Cells = ",    $inactive;}     // Driver Code    $cells= array(0, 1, 0, 1, 0, 1, 0, 1);    $k = 3;    $n = count($cells);    activeAndInactive($cells, $n, $k);     // This code is contributed by anuj_67.?>
<script>// javascript program to count active and// inactive cells after k days    // cells - store current status    // of cells n - Number of cells    // temp - to perform intermediate operations    // k - number of days    // active - count of active cells after k days    // inactive - count of active cells after k days    function activeAndInactive(cells , n , k)    {             // copy cells array into temp  array        var temp =  Array(n).fill(false);        for (i = 0; i < n; i++)            temp[i] = cells[i];         // Iterate for k days        while (k-- > 0)        {             // Finding next values for corner cells            temp[0] = false ^ cells[1];            temp[n - 1] = false ^ cells[n - 2];             // Compute values of intermediate cells            // If both cells active or inactive, then            // temp[i]=0 else temp[i] = 1.            for (i = 1; i <= n - 2; i++)                temp[i] = cells[i - 1] ^ cells[i + 1];             // Copy temp to cells for next iteration            for (i = 0; i < n; i++)                cells[i] = temp[i];        }         // count active and inactive cells        var active = 0, inactive = 0;        for (i = 0; i < n; i++)            if (cells[i] == true)                active++;            else                inactive++;         document.write("Active Cells = " + active + ", " + "Inactive Cells = " + inactive);    }     // Driver code        var cells = [ false, true, false, true, false, true, false, true ];        var k = 3;        var n = cells.length;        activeAndInactive(cells, n, k); // This code is contributed by Rajput-Ji</script>
Output:  
Active Cells = 2 , Inactive Cells = 6
This article is contributed by Shashank Mishra (Gullu). This article is reviewed by team geeksforgeeks. If you have better approach for this problem please share. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
vt_m
Rajput-Ji
Bitwise-XOR
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Chocolate Distribution Problem
Window Sliding Technique
Reversal algorithm for array rotation
Next Greater Element
Find duplicates in O(n) time and O(1) extra space | Set 1
Count pairs with given sum
Find subarray with given sum | Set 1 (Nonnegative Numbers)
Building Heap from Array
Remove duplicates from sorted array
Program to find sum of elements in a given array