957. Prison Cells After N Days LeetCode Solution
In this guide, you will get 957. Prison Cells After N Days LeetCode Solution with the best time and space complexity. The solution to Prison Cells After N Days problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Prison Cells After N Days solution in C++
- Prison Cells After N Days solution in Java
- Prison Cells After N Days solution in Python
- Additional Resources

Problem Statement of Prison Cells After N Days
There are 8 prison cells in a row and each cell is either occupied or vacant.
Each day, whether the cell is occupied or vacant changes according to the following rules:
If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.
Otherwise, it becomes vacant.
Note that because the prison is a row, the first and the last cells in the row can’t have two adjacent neighbors.
You are given an integer array cells where cells[i] == 1 if the ith cell is occupied and cells[i] == 0 if the ith cell is vacant, and you are given an integer n.
Return the state of the prison after n days (i.e., n such changes described above).
Example 1:
Input: cells = [0,1,0,1,1,0,0,1], n = 7
Output: [0,0,1,1,0,0,0,0]
Explanation: The following table summarizes the state of the prison on each day:
Day 0: [0, 1, 0, 1, 1, 0, 0, 1]
Day 1: [0, 1, 1, 0, 0, 0, 0, 0]
Day 2: [0, 0, 0, 0, 1, 1, 1, 0]
Day 3: [0, 1, 1, 0, 0, 1, 0, 0]
Day 4: [0, 0, 0, 0, 0, 1, 0, 0]
Day 5: [0, 1, 1, 1, 0, 1, 0, 0]
Day 6: [0, 0, 1, 0, 1, 1, 0, 0]
Day 7: [0, 0, 1, 1, 0, 0, 0, 0]
Example 2:
Input: cells = [1,0,0,1,0,0,1,0], n = 1000000000
Output: [0,0,1,1,1,1,1,0]
Constraints:
cells.length == 8
cells[i] is either 0 or 1.
1 <= n <= 109
Complexity Analysis
- Time Complexity:
- Space Complexity: O(1)
957. Prison Cells After N Days LeetCode Solution in C++
class Solution {
public:
vector<int> prisonAfterNDays(vector<int>& cells, int n) {
vector<int> firstDayCells;
vector<int> nextDayCells(cells.size());
for (int day = 0; n-- > 0; cells = nextDayCells, ++day) {
for (int i = 1; i + 1 < cells.size(); ++i)
nextDayCells[i] = cells[i - 1] == cells[i + 1];
if (day == 0)
firstDayCells = nextDayCells;
else if (nextDayCells == firstDayCells)
n %= day;
}
return cells;
}
};
/* code provided by PROGIEZ */
957. Prison Cells After N Days LeetCode Solution in Java
class Solution {
public int[] prisonAfterNDays(int[] cells, int n) {
int[] firstDayCells = new int[cells.length];
int[] nextDayCells = new int[cells.length];
for (int day = 0; n-- > 0; cells = nextDayCells.clone(), ++day) {
for (int i = 1; i + 1 < cells.length; ++i)
nextDayCells[i] = cells[i - 1] == cells[i + 1] ? 1 : 0;
if (day == 0)
firstDayCells = nextDayCells.clone();
else if (Arrays.equals(nextDayCells, firstDayCells))
n %= day;
}
return cells;
}
}
// code provided by PROGIEZ
957. Prison Cells After N Days LeetCode Solution in Python
class Solution:
def prisonAfterNDays(self, cells: list[int], n: int) -> list[int]:
nextDayCells = [0] * len(cells)
day = 0
while n > 0:
n -= 1
for i in range(1, len(cells) - 1):
nextDayCells[i] = 1 if cells[i - 1] == cells[i + 1] else 0
if day == 0:
firstDayCells = nextDayCells.copy()
elif nextDayCells == firstDayCells:
n %= day
cells = nextDayCells.copy()
day += 1
return cells
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.