Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
Syntax
void activeCellKdays(int arr[], int n, int k)
Algorithm
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
Here is the complete C program to simulate the active and inactive cells after k days −
#include <stdio.h>
void activeCellKdays(int arr[], int n, int k) {
int temp[n]; // temp is holding the copy of the arr
for (int day = 0; day < k; day++) {
// Copy current array to temp for processing
for (int i = 0; i < n; i++)
temp[i] = arr[i];
// Set value for leftmost cell
temp[0] = 0 ^ arr[1];
// Set value for rightmost cell
temp[n-1] = 0 ^ arr[n-2];
// For all intermediate cells: if left and right are not same, put 1
for (int i = 1; i <= n-2; i++)
temp[i] = arr[i-1] ^ arr[i+1];
// Copy back the temp to arr for the next iteration
for (int i = 0; i < n; i++)
arr[i] = temp[i];
}
// Count active and inactive cells
int active = 0, inactive = 0;
for (int i = 0; i < n; i++) {
if (arr[i])
active++;
else
inactive++;
}
printf("Active Cells = %d, Inactive Cells = %d
", active, inactive);
}
int main() {
int arr[] = {0, 1, 0, 1, 0, 1, 0, 1};
int k = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printf("Initial array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("
");
activeCellKdays(arr, n, k);
return 0;
}
Output
Initial array: 0 1 0 1 0 1 0 1 Active Cells = 2, Inactive Cells = 6
How It Works
The algorithm works as follows −
- XOR Operation: For each cell, we check if its left and right neighbors are different using XOR operation. If they are different, XOR returns 1 (active), otherwise 0 (inactive).
- Boundary Cells: The leftmost and rightmost cells are always treated as having 0 neighbors on their outer sides.
- Simulation: We simulate k days by repeating the process k times, using a temporary array to store intermediate results.
Conclusion
This problem demonstrates cellular automaton simulation using XOR operations. The key insight is that each cell's next state depends only on its immediate neighbors' current states, making it perfect for XOR-based computation.
