Count Total Number of Colored Cells - Problem

There exists an infinitely large two-dimensional grid of uncolored unit cells. You are given a positive integer n, indicating that you must do the following routine for n minutes:

At the first minute, color any arbitrary unit cell blue.

Every minute thereafter, color blue every uncolored cell that touches a blue cell.

Return the number of colored cells at the end of n minutes.

Input & Output

Example 1 — Basic Case
$ Input: n = 1
Output: 1
💡 Note: At minute 1, we color the initial cell. Total colored cells = 1.
Example 2 — Small Diamond
$ Input: n = 2
Output: 5
💡 Note: Minute 1: 1 cell. Minute 2: original cell + 4 adjacent cells = 5 total.
Example 3 — Growing Pattern
$ Input: n = 3
Output: 13
💡 Note: Forms diamond shape: 1 + 4 + 8 = 13 cells after 3 minutes.

Constraints

  • 1 ≤ n ≤ 105

Visualization

Tap to expand
Cell Coloring Process: From Single Cell to Diamond PatternInput: n = 33minutesProcess: Spreading PatternMinute 11 cellMinute 25 cellsMinute 313 cellsOutput: 1313total cellsDiamond Pattern: Each minute adds a new ring of cells around the existing onesFormula: 2n² - 2n + 1 = 2(9) - 2(3) + 1 = 18 - 6 + 1 = 13Original (Min 1)Added Min 2Added Min 3
Understanding the Visualization
1
Input
Number of minutes n = 3
2
Process
Cells spread outward each minute forming diamond
3
Output
Total colored cells = 13
Key Takeaway
🎯 Key Insight: The spreading pattern forms a predictable diamond shape that can be calculated using the mathematical formula 2n² - 2n + 1
Asked in
Google 25 Microsoft 18 Amazon 15
25.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen