Minimum Number of Days to Disconnect Island - Problem

You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1's.

The grid is said to be connected if we have exactly one island, otherwise is said disconnected.

In one day, we are allowed to change any single land cell (1) into a water cell (0).

Return the minimum number of days to disconnect the grid.

Input & Output

Example 1 — Single Critical Point
$ Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Output: 2
💡 Note: The grid forms one connected island. No single cell removal disconnects it, so we need to remove 2 cells to create separation.
Example 2 — Already Disconnected
$ Input: grid = [[1,1,0,1,1],[1,1,1,1,1],[1,1,0,1,1],[1,1,1,1,1]]
Output: 1
💡 Note: Removing the center cell at (1,2) or (2,2) would disconnect the island into multiple parts.
Example 3 — Small Island
$ Input: grid = [[1,0,1,0]]
Output: 0
💡 Note: There are already 2 separate islands, so the grid is already disconnected.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 30
  • grid[i][j] is either 0 or 1

Visualization

Tap to expand
Minimum Days to Disconnect Island INPUT Binary Grid (m x n) 0 1 1 0 0 1 1 0 0 0 0 0 = Land (1) = Water (0) Input Values: grid = [[0,1,1,0], [0,1,1,0], [0,0,0,0]] ALGORITHM STEPS 1 Check if Disconnected Count islands using BFS/DFS If islands != 1, return 0 2 Find Articulation Point Try removing each land cell If disconnects, return 1 3 Mathematical Insight Any island can be split by removing corner in 2 days 4 Return Result Answer is always 0, 1, or 2 If not 0 or 1, return 2 --> FINAL RESULT After 2 Days - Disconnected 0 0 1 0 0 0 1 0 0 0 0 0 = Removed (Day 1, 2) OUTPUT 2 OK - Grid disconnected in minimum 2 days Key Insight: The answer is always 0, 1, or 2. If the grid is already disconnected, return 0. If removing any single land cell disconnects it (articulation point), return 1. Otherwise, any 2x2 block of land can always be disconnected by removing 2 corner cells, so the maximum answer is 2. Time: O(n^2 * m^2) TutorialsPoint - Minimum Number of Days to Disconnect Island | Optimized Mathematical Insight
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
28.5K Views
Medium Frequency
~25 min Avg. Time
856 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