Max Area of Island — Solution in Python, Java & C++
Max Area of Island solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem, asked at Intuit.
Max Area of Island is a medium coding problem on arrays, depth-first search and matrix, asked in Intuit interviews.
- Difficulty: Medium
- Topics: Arrays, Depth-First Search, Matrix
- Asked at: Intuit
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Given an m x n binary grid, return the area of the largest island (group of 1s connected 4-directionally). Return 0 if there is no island.
Example 1
Input: grid = [[1,1,0,0],[1,0,0,1],[0,0,1,1],[0,0,0,1]]
Output: 4
Example 2
Input: grid = [[0,0,0,0]]
Output: 0
Constraints
1 <= m, n <= 8grid[i][j] is 0 or 1.
Hints for Max Area of Island
Hint 1
DFS from each land cell returns the island's size while sinking it.
Hint 2
Track the maximum size across all starting cells.
How to solve Max Area of Island
Same island sweep as counting them, except each flood returns the size of the component it consumed instead of just marking it. Track the largest size seen across all starting cells.
Approach
- Scan every cell; skip anything that is not fresh land.
- From a fresh land cell, flood the whole component, sinking each cell as it is pushed and counting how many you sink.
- Compare that count with the best so far.
- Continue the scan and return the maximum, or
0if the grid holds no land.
Why it works
Because the flood sinks each cell exactly once, the count it returns is precisely the number of cells in that connected component — no cell is counted twice and none is missed. Every island is reached by the outer scan exactly once, so the maximum over all floods is the largest island. A grid with no land never enters the flood, leaving the initial 0, which is the required answer.
Complexity
- Time —
O(rows · cols) - Space —
O(rows · cols)
Pitfalls
- Count a cell when you sink it — counting on pop plus pushing duplicates would inflate the area.
- Reset the area counter for each new island; carrying it over sums unrelated islands together.
- An empty grid must return
0, not a sentinel. - Sink on push so no cell is enqueued twice.
Reference solution
Python
from typing import List
def maxAreaOfIsland(grid: List[List[int]]) -> int:
g = [row[:] for row in grid]
def area(i, j):
if i < 0 or j < 0 or i >= len(g) or j >= len(g[0]) or g[i][j] != 1:
return 0
g[i][j] = 0
return 1 + area(i + 1, j) + area(i - 1, j) + area(i, j + 1) + area(i, j - 1)
best = 0
for i in range(len(g)):
for j in range(len(g[0])):
best = max(best, area(i, j))
return bestJava
public static int maxAreaOfIsland(int[][] grid) {
int rows = grid.length, cols = grid[0].length, best = 0;
int[] dr = {-1, 1, 0, 0};
int[] dc = {0, 0, -1, 1};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] != 1) continue;
int area = 0;
Deque<int[]> stack = new ArrayDeque<>();
stack.push(new int[]{i, j});
grid[i][j] = 0;
while (!stack.isEmpty()) {
int[] cell = stack.pop();
area++;
for (int d = 0; d < 4; d++) {
int nr = cell[0] + dr[d], nc = cell[1] + dc[d];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 1) {
grid[nr][nc] = 0;
stack.push(new int[]{nr, nc});
}
}
}
if (area > best) best = area;
}
}
return best;
}C++
int maxAreaOfIsland(vector<vector<int>>& grid) {
int rows = (int) grid.size(), cols = (int) grid[0].size(), best = 0;
int dr[4] = {-1, 1, 0, 0};
int dc[4] = {0, 0, -1, 1};
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] != 1) continue;
int area = 0;
vector<pair<int, int>> stack;
stack.push_back(make_pair(i, j));
grid[i][j] = 0;
while (!stack.empty()) {
pair<int, int> cell = stack.back();
stack.pop_back();
area++;
for (int d = 0; d < 4; d++) {
int nr = cell.first + dr[d], nc = cell.second + dc[d];
if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && grid[nr][nc] == 1) {
grid[nr][nc] = 0;
stack.push_back(make_pair(nr, nc));
}
}
}
if (area > best) best = area;
}
}
return best;
}JavaScript
var maxAreaOfIsland = function(grid) {
const g = grid.map(function(r) { return r.slice(); });
function area(i, j) {
if (i < 0 || j < 0 || i >= g.length || j >= g[0].length || g[i][j] !== 1) return 0;
g[i][j] = 0;
return 1 + area(i + 1, j) + area(i - 1, j) + area(i, j + 1) + area(i, j - 1);
}
let best = 0;
for (let i = 0; i < g.length; i++) {
for (let j = 0; j < g[0].length; j++) {
best = Math.max(best, area(i, j));
}
}
return best;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Depth-First Search (DFS)