Browse curriculum

Making A Large Island

Find the largest island you can make by turning at most one 0 into a 1, by labeling islands with ids and sizes and then scoring each water cell from its neighbors.

Problem Understanding

Making A Large Island: in an n × n binary grid you may change at most one 0 to 1. Return the size of the largest island (4-directionally connected 1s) afterwards.

Examples: [[1,0],[0,1]] → 3; [[1,1],[1,0]] → 4; [[1,1],[1,1]] → 4.

The Intuition: Precompute Islands, Then Each Flip Is a Sum

Flipping a 0 joins every island that touches it. So:

  1. Label every island with a DFS, as in Number of Islands, but also record each island's size under its id.
  2. Score each 0: 1 for the flipped cell plus the sizes of the distinct neighboring islands.
  3. Take the best, or the largest existing island if there is nothing to flip.

Merging groups and counting their size is also what union-find does in Number of Operations to Make Network Connected.

Interactive Walkthrough

LAND SHOWS ITS ISLAND ID · · = WATER · + = THE FLIP BEING TRIED

LABEL THE ISLANDS

islands

0

this flip

:

best

:

 

Pass 1: give every island an id (2, 3, …) and record its size

Next

Island 2 starts at (0, 0): DFS marks its 3 cells with 2

Free preview: the first 3 of 10 steps

grid

Land cells show their island id and color; water is a dot. On each try the flipped cell is marked + and the islands it joins are emphasized. The default grid shows one flip touching the same island twice.

Dry Run Table

Input: [[1,1,0,0],[1,0,1,1],[0,0,1,0]]. Island 2 = {(0,0),(0,1),(1,0)}, size 3; island 3 = {(1,2),(1,3),(2,2)}, size 3.

Flip Neighbor islands Score
(0,2) 3, 2 1 + 3 + 3 = 7
(0,3) 3 4
(1,1) 2 (twice), 3 1 + 3 + 3 = 7
(2,0) 2 4
(2,1) 3 4
(2,3) 3 4

Answer: 7.

The Solution Template

Label with DFS, then score each water cell. The grid is modified in place to store island ids.

Making A Large Island Code
1var largestIsland = function(grid) {
2 const n = grid.length, m = grid[0].length;
3 const sizes = new Map();
4 const fill = (r, c, island) => {
5 if (r < 0 || c < 0 || r >= n || c >= m || grid[r][c] !== 1) return 0;
6 grid[r][c] = island;
7 return 1 + fill(r + 1, c, island) + fill(r - 1, c, island) + fill(r, c + 1, island) + fill(r, c - 1, island);
8 };
9 let island = 2, best = 0;
10 for (let r = 0; r < n; r++)
11 for (let c = 0; c < m; c++)
12 if (grid[r][c] === 1) {
13 const size = fill(r, c, island);
14 sizes.set(island++, size);
15 best = Math.max(best, size);
16 }
17 for (let r = 0; r < n; r++)
18 for (let c = 0; c < m; c++)
19 if (grid[r][c] === 0) {
20 const seen = new Set();
21 for (const [nr, nc] of [[r + 1, c], [r - 1, c], [r, c + 1], [r, c - 1]])
22 if (nr >= 0 && nc >= 0 && nr < n && nc < m && grid[nr][nc] > 1) seen.add(grid[nr][nc]);
23 let total = 1;
24 for (const k of seen) total += sizes.get(k);
25 best = Math.max(best, total);
26 }
27 return best;
28};

Edge Cases & Common Mistakes

  • No zeros: answer is n²; initialize best with the largest island.
  • All zeros: answer is 1.
  • Double counting: use a set of neighbor ids.
  • Deep recursion: a 500 × 500 snake-shaped island can overflow the call stack; use an explicit stack in production.
  • Using 1 as an id: clashes with unvisited land; start ids at 2.

Summary

Label each island with an id and size via DFS, then for each 0 add 1 to the sizes of its distinct neighboring islands; return the maximum (or the largest island if there is no 0). O(R·C) time and space.

More practice: DSA interview questions with answers · patterns cheat sheet

The approach, step by step

  1. Label

    DFS from each unvisited 1, writing a new id (2, 3, ...) into its cells and recording the island's size.

  2. Score each 0

    Collect the distinct ids among its four neighbors; the score is 1 plus their sizes.

  3. Answer

    Return the best score, or the largest island if there is no 0.

Frequently asked questions

Why label islands first instead of trying every flip?

Flipping each 0 and re-measuring the island with a DFS costs O(R·C) per flip, O((R·C)²) in total. Labeling every island once with an id and a size makes each flip an O(1) lookup: just add the sizes of the islands around it.


Why use a set of neighbor ids?

The same island can touch a water cell on two or more sides, like an island that wraps around a corner. Without a set its size would be counted twice. Collecting the distinct ids first avoids that.


Why do island ids start at 2?

0 and 1 already mean water and unvisited land in the grid. Starting ids at 2 lets the DFS write the id straight into the grid, which marks cells as visited and remembers their island at the same time.


What if the grid has no 0?

Then there is nothing to flip and the whole grid is a single island, so the answer is R·C. Initializing best with the largest island size handles this, and also grids where flipping never beats an existing island.

New to the technique behind this problem? Watch it run step by step:

Depth First Search Visualization

Applying for software engineering roles? Free, no sign-up:

AI resume checker: match your resume to the job description