- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Active and Inactive cells after k Days?

Here we will see one interesting problem. Suppose one binary array is given of size n. Here n > 3. A true value or 1 value indicates that the active state, and 0 or false indicates inactive. Another number k is also given. We have to find active or inactive cells after k days. After every day state of ith cell will be active if the left and right cells are not same, if they are same, then it will be inactive. The left most and right most cell has no cell before and after it. So left most and right most cells are always 0.

Let us see one example to get the idea. Suppose one array is like {0, 1, 0, 1, 0, 1, 0, 1}, and the value of k = 3. So let us see how it changes day by day.

- After 1 day the array will be {1, 0, 0, 0, 0, 0, 0, 0}
- After 2 days the array will be {0, 1, 0, 0, 0, 0, 0, 0}
- After 3 days the array will be {1, 0, 1, 0, 0, 0, 0, 0}

So 2 active cells and 6 inactive cells

## Algorithm

#### activeCellKdays(arr, n, k)

begin make a copy of arr into temp for i in range 1 to k, do temp[0] := 0 XOR arr[1] temp[n-1] := 0 XOR arr[n-2] for each cell i from 1 to n-2, do temp[i] := arr[i-1] XOR arr[i+1] done copy temp to arr for next iteration done count number of 1s as active, and number of 0s as inactive, then return the values. end

## Example

#include <iostream> using namespace std; void activeCellKdays(bool arr[], int n, int k) { bool temp[n]; //temp is holding the copy of the arr for (int i=0; i<n ; i++) temp[i] = arr[i]; for(int i = 0; i<k; i++){ temp[0] = 0^arr[1]; //set value for left cell temp[n-1] = 0^arr[n-2]; //set value for right cell for (int i=1; i<=n-2; i++) //for all intermediate cell if left and right are not same, put 1 temp[i] = arr[i-1] ^ arr[i+1]; for (int i=0; i<n; i++) arr[i] = temp[i]; //copy back the temp to arr for the next iteration } int active = 0, inactive = 0; for (int i=0; i<n; i++) if (arr[i]) active++; else inactive++; cout << "Active Cells = "<< active <<", Inactive Cells = " << inactive; } main() { bool arr[] = {0, 1, 0, 1, 0, 1, 0, 1}; int k = 3; int n = sizeof(arr)/sizeof(arr[0]); activeCellKdays(arr, n, k); }

## Output

Active Cells = 2, Inactive Cells = 6

- Related Questions & Answers
- Program to find state of prison cells after k days in python
- Prison Cells After N Days in C++
- Deleting inactive objects in SAP HANA
- C++ code to find tree height after n days
- Display dates after NOW() + 10 days from a MySQL table?
- Distinguish between active and passive investment.
- Median after K additional integers in C++
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Maximize array sum after K negation in C++
- Difference between Active Attack and Passive Attack
- Ever Wondered Why do we celebrate Dasara after Nine Days of fast and Worship Goddess Durga?
- C program to convert days into months and number of days
- K and -K in Python
- How to find the date after a number of days in R?
- Maximize Sum Of Array After K Negations in Python

Advertisements