Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

957. Prison Cells After N Days

Medium39.0% Acceptance
ArrayHash TableMath
Asked by:
S
SAP
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

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
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions

Approach

In #957 Prison Cells After N Days, each day's state depends only on the previous configuration of eight cells. A key observation is that the first and last cells always become 0, meaning only the middle six cells actually change. This creates a limited number of possible states, which leads to a repeating cycle.

The common strategy is to simulate the cell transitions day by day while storing previously seen states in a hash table. Once a state repeats, a cycle is detected. Instead of simulating all N days, you can compute N % cycle_length and only simulate the remaining days. This drastically reduces the number of operations when N is very large.

An optimized implementation can represent the cells using bit manipulation, converting the 8-cell array into a bitmask for faster transitions and compact storage. The approach leverages pattern repetition to avoid unnecessary simulation.

Complexity

ApproachTime ComplexitySpace Complexity
Simulation with Hash Set (Cycle Detection)O(min(N, 64))O(64)
Bitmask + Cycle DetectionO(min(N, 64))O(1)

Video Solution Available

Techdose

View all video solutions

Solutions (12)

Simulate Each Day

This approach involves simulating each day's changes directly. Given the constraints of the problem, this can be manageable for relatively small values of 'n'. We iterate over each day, applying the rules to determine the state of each cell. Importantly, the edge cells are always vacant as they lack two neighbors. By updating the state iteratively, we can reach the final configuration after 'n' days.

Time complexity: O(1), as the complexity per cycle of states is fixed. During each cycle, we just iterate over the 8 states.
Space complexity: O(1), only a fixed array is used for state computation.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <string.h>
3
4void nextDay(int *cells, int *nextCells) {
5    nextCells

Explanation

The C program defines a function nextDay to calculate the state of cells for the next day based on the current state using the rules provided. The prisonAfterNDays function uses this helper function to simulate day-by-day changes. The logic accommodates the cyclic nature of the problem by using n % 14 to optimize large iterations, where 14 is the length of the repeating cycle determined by observation and testing.

Identify Cycle in States

This alternative approach builds on recognizing patterns within the transformations. Given only 8 cells and binary states, the maximum number of possible unique states is 27 = 128. Through simulation, we can identify that the pattern cycles or repeats after at most 14 days, reducing the need to simulate all given 'n' days. By finding cycles, one can compute the state that would correspond to the final day directly, drastically minimizing operations.

Time complexity: O(128), as we explore states to identify cycles, bounded by maximum unique states of 128.
Space complexity: O(128), accommodates tracking up to 128 states.

CC++JavaPythonC#JavaScript



Video Solutions

Watch expert explanations and walkthroughs

Prison Cells After N Days | Leetcode #957

Techdose
18:0113,724 views

Asked By Companies

1 companies
S
SAP

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Valid SudokuMedium
Sudoku SolverHard
First Missing PositiveHard
More similar problems

Related Topics

ArrayHash TableMathBit Manipulation

Problem Stats

Acceptance Rate39.0%
DifficultyMedium
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why does Prison Cells After N Days form a cycle?

The problem has a fixed number of cells and deterministic rules, which means the number of possible states is finite. After several transitions, a previously seen state must appear again. This repetition creates a cycle in the sequence of states.

Is Prison Cells After N Days asked in coding interviews?

Yes, this problem is a popular medium-level interview question. It tests understanding of arrays, simulation, hash tables, and recognizing repeating patterns or cycles in state transitions.

What data structure is best for Prison Cells After N Days?

A hash set or hash map is commonly used to store previously seen cell states. This helps quickly detect cycles in the simulation. Some optimized solutions also use bitmasks to represent the cells efficiently.

What is the optimal approach for Prison Cells After N Days?

The optimal approach uses cycle detection while simulating the cell states. Since the number of possible configurations is limited, the states eventually repeat. Once a cycle is found, you can reduce N using modulo arithmetic and simulate only the remaining days.

[
0
]
=
0
;
6
nextCells
[
7
]
=
0
;
7
for
(
int
i
=
1
;
i
<
7
;
++
i
)
{
8
nextCells
[
i
]
=
cells
[
i
-
1
]
==
cells
[
i
+
1
]
;
9
}
10
}
11
12
void
prisonAfterNDays
(
int
*
cells
,
int
n
,
int
*
result
)
{
13
int
nextCells
[
8
]
;
14
n
=
n
%
14
==
0
?
14
:
n
%
14
;
// Optimization purpose
15
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
16
nextDay
(
cells
,
nextCells
)
;
17
memcpy
(
cells
,
nextCells
,
8
*
sizeof
(
int
)
)
;
18
}
19
memcpy
(
result
,
cells
,
8
*
sizeof
(
int
)
)
;
20
}
21
22
int
main
(
)
{
23
int
cells
[
8
]
=
{
0
,
1
,
0
,
1
,
1
,
0
,
0
,
1
}
;
24
int
n
=
7
;
25
int
result
[
8
]
;
26
prisonAfterNDays
(
cells
,
n
,
result
)
;
27
for
(
int
i
=
0
;
i
<
8
;
i
++
)
{
28
printf
(
"%d "
,
result
[
i
]
)
;
29
}
30
return
0
;
31
}
1
#
include
<stdio.h>
2
#
include
<string.h>
3
4
int
findCycle
(
int
*
cells
)
{
5
int
seen
[
256
]
[
8
]
,
cycleLength
=
0
;
6
while
(
cycleLength
<
256
)
{
7
for
(
int
i
=
1
;
i
<
7
;
++
i
)
{
8
cells
[
i
]
=
cells
[
i
-
1
]
==
cells
[
i
+
1
]
;
9
}
10
cells
[
0
]
=
cells
[
7
]
=
0
;
11
int
found
=
0
;
12
for
(
int
i
=
0
;
i
<
cycleLength
;
++
i
)
{
13
if
(
memcmp
(
seen
[
i
]
,
cells
,
8
*
sizeof
(
int
)
)
==
0
)
found
=
1
;
14
}
15
if
(
found
)
break
;
16
memcpy
(
seen
[
cycleLength
++
]
,
cells
,
8
*
sizeof
(
int
)
)
;
17
}
18
return
cycleLength
;
19
}
20
21
void
prisonAfterNDays
(
int
*
cells
,
int
n
)
{
22
int
cycle
=
findCycle
(
cells
)
;
23
n
=
n
%
cycle
==
0
?
cycle
:
n
%
cycle
;
24
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
25
int
newCells
[
8
]
;
26
for
(
int
j
=
1
;
j
<
7
;
++
j
)
{
27
newCells
[
j
]
=
cells
[
j
-
1
]
==
cells
[
j
+
1
]
;
28
}
29
newCells
[
0
]
=
newCells
[
7
]
=
0
;
30
memcpy
(
cells
,
newCells
,
8
*
sizeof
(
int
)
)
;
31
}
32
}
33
34
int
main
(
)
{
35
int
cells
[
8
]
=
{
0
,
1
,
0
,
1
,
1
,
0
,
0
,
1
}
;
36
int
n
=
7
;
37
prisonAfterNDays
(
cells
,
n
)
;
38
for
(
int
i
=
0
;
i
<
8
;
i
++
)
{
39
printf
(
"%d "
,
cells
[
i
]
)
;
40
}
41
return
0
;
42
}

Explanation

The C code leverages a direct approach to identify patterns using the findCycle method, where state sequences are preserved and revisited to determine recurring configurations. This understanding is then used to restrict operations to only necessary repetitions through precise cycle length determination.