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

695. Max Area of Island

Medium72.6% Acceptance
ArrayDepth-First SearchBreadth-First Search
Asked by:
Amazon
ProblemSolutions (12)VideosCompanies (9)Notes

Problem Statement

You are given an m x n binary matrix grid. An island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in grid. If there is no island, return 0.

Example 1:

Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]
Output: 6
Explanation: The answer is not 11, because the island must be connected 4-directionally.

Example 2:

Input: grid = [[0,0,0,0,0,0,0,0]]
Output: 0

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 50
  • grid[i][j] is either 0 or 1.
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
A
F
Facebook
M
Microsoft
G
Google
Q
Qualtrics
+4

Approach

In #695 Max Area of Island, you are given a binary matrix where 1 represents land and 0 represents water. The goal is to compute the maximum area of a connected island. Two cells belong to the same island if they are connected horizontally or vertically.

The most common approach is to use Depth-First Search (DFS) or Breadth-First Search (BFS). Iterate through every cell in the grid, and whenever you encounter an unvisited land cell (1), start a traversal to explore all connected land cells. Mark visited cells to avoid reprocessing and count the total cells in that island. Track the maximum area encountered during the traversal.

Another possible method uses the Union Find (Disjoint Set) data structure to group adjacent land cells into connected components and maintain their sizes.

The DFS/BFS traversal visits each cell at most once, giving a time complexity of O(m × n) and space complexity depending on recursion or queue usage.

Complexity

ApproachTime ComplexitySpace Complexity
DFS / BFS TraversalO(m × n)O(m × n) worst case due to recursion stack or queue
Union FindO(m × n α(m × n))O(m × n)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Depth First Search (DFS) Approach

In this approach, we will use Depth First Search (DFS) to explore the grid. The idea is to iterate over each cell in the grid and apply DFS when we encounter an unvisited cell that is a part of an island (i.e., grid value is 1). We will mark each visited cell to avoid revisiting. By keeping a count of the size of each island found, we can track the maximum size encountered.

Time Complexity: O(m * n), where m is the number of rows and n is the number of columns. Each cell is visited once.
Space Complexity: O(m * n), used for the visited matrix and recursion stack.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2
3#define MAX 50
4
5int dfs(int grid[MAX][MAX], int visited

Explanation

This C solution defines a DFS function that recursively counts connected land cells. A visited matrix is used to ensure cells are only counted once. The main function iterates over all cells in the grid and updates the maximum area found.

Breadth First Search (BFS) Approach

A Breadth First Search (BFS) approach can also be used to solve this problem by iteratively exploring each cell's neighbors using a queue and counting the size of islands found. By enqueueing all land cell neighbors for exploration and tracking maximum sizes found, we can also determine the maximum island area.

Time Complexity: O(m * n), since all cells are enqueued and dequeued once.
Space Complexity: O(m * n), required for the queue and the visited matrix.

CC++JavaPythonC#JavaScript
1










Video Solutions

Watch expert explanations and walkthroughs

NUMBER OF ISLANDS - Leetcode 200 - Python

NeetCode
11:41384,140 views

Asked By Companies

9 companies
A
Amazon
F
Facebook
M
Microsoft
G
Google
Q
Qualtrics
L
LinkedIn
A
Apple
D
Dropbox
O
Oracle

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

Surrounded RegionsMedium
Number of IslandsMedium
Alien DictionaryHard
Smallest Rectangle Enclosing Black PixelsHard
More similar problems

Related Topics

ArrayDepth-First SearchBreadth-First SearchUnion FindMatrix

Problem Stats

Acceptance Rate72.6%
DifficultyMedium
Companies9

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is Max Area of Island asked in FAANG interviews?

Yes, grid traversal problems like Max Area of Island are commonly asked in FAANG and other top tech interviews. They test understanding of DFS, BFS, graph traversal, and matrix-based problem solving.

What data structure is best for Max Area of Island?

Graphs represented through grid traversal are typically solved using recursion or a queue with DFS or BFS. Alternatively, Union Find can be used to group adjacent land cells and maintain island sizes efficiently.

What is the optimal approach for Max Area of Island?

The optimal approach is to use DFS or BFS traversal. Start exploring whenever you encounter a land cell and count all connected land cells while marking them visited. This ensures each cell is processed once, resulting in O(m × n) time complexity.

Why do we mark cells as visited in Max Area of Island?

Marking cells as visited prevents counting the same land cell multiple times and avoids infinite loops during traversal. It ensures each cell contributes to the island area exactly once.

[
MAX
]
[
MAX
]
,
int
m
,
int
n
,
int
i
,
int
j
)
{
6
if
(
i
<
0
||
i
>=
m
||
j
<
0
||
j
>=
n
||
grid
[
i
]
[
j
]
==
0
||
visited
[
i
]
[
j
]
)
{
7
return
0
;
8
}
9
visited
[
i
]
[
j
]
=
1
;
10
return
1
+
dfs
(
grid
,
visited
,
m
,
n
,
i
+
1
,
j
)
11
+
dfs
(
grid
,
visited
,
m
,
n
,
i
-
1
,
j
)
12
+
dfs
(
grid
,
visited
,
m
,
n
,
i
,
j
+
1
)
13
+
dfs
(
grid
,
visited
,
m
,
n
,
i
,
j
-
1
)
;
14
}
15
16
int
maxAreaOfIsland
(
int
grid
[
MAX
]
[
MAX
]
,
int
m
,
int
n
)
{
17
int
visited
[
MAX
]
[
MAX
]
=
{
0
}
;
18
int
max_area
=
0
;
19
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
20
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
21
if
(
grid
[
i
]
[
j
]
==
1
&&
!
visited
[
i
]
[
j
]
)
{
22
int
area
=
dfs
(
grid
,
visited
,
m
,
n
,
i
,
j
)
;
23
if
(
area
>
max_area
)
{
24
max_area
=
area
;
25
}
26
}
27
}
28
}
29
return
max_area
;
30
}
31
32
int
main
(
)
{
33
int
grid
[
MAX
]
[
MAX
]
=
{
{
0
,
0
,
1
,
0
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
0
,
0
}
,
34
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
0
,
0
,
0
}
,
35
{
0
,
1
,
1
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
}
,
36
{
0
,
1
,
0
,
0
,
1
,
1
,
0
,
0
,
1
,
0
,
1
,
0
,
0
}
,
37
{
0
,
1
,
0
,
0
,
1
,
1
,
0
,
0
,
1
,
1
,
1
,
0
,
0
}
,
38
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
0
,
0
}
,
39
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
0
,
0
,
0
}
,
40
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
0
,
0
,
0
,
0
}
}
;
41
int
m
=
8
,
n
=
13
;
42
printf
(
"Max area of island: %d"
,
maxAreaOfIsland
(
grid
,
m
,
n
)
)
;
43
return
0
;
44
}
#
include
<stdio.h>
2
#
include
<stdlib.h>
3
4
#
define
MAX
50
5
6
typedef
struct
{
7
int
x
,
y
;
8
}
Point
;
9
10
typedef
struct
{
11
Point
*
data
;
12
int
front
,
rear
,
size
;
13
}
Queue
;
14
15
Queue
*
createQueue
(
int
maxSize
)
{
16
Queue
*
queue
=
(
Queue
*
)
malloc
(
sizeof
(
Queue
)
)
;
17
queue
->
data
=
(
Point
*
)
malloc
(
maxSize
*
sizeof
(
Point
)
)
;
18
queue
->
front
=
queue
->
rear
=
queue
->
size
=
0
;
19
return
queue
;
20
}
21
22
void
enqueue
(
Queue
*
queue
,
Point item
)
{
23
queue
->
data
[
queue
->
rear
++
]
=
item
;
24
queue
->
size
++
;
25
}
26
27
Point
dequeue
(
Queue
*
queue
)
{
28
queue
->
size
--
;
29
return
queue
->
data
[
queue
->
front
++
]
;
30
}
31
32
int
bfs
(
int
grid
[
MAX
]
[
MAX
]
,
int
visited
[
MAX
]
[
MAX
]
,
int
m
,
int
n
,
int
sx
,
int
sy
)
{
33
int
directions
[
4
]
[
2
]
=
{
{
1
,
0
}
,
{
-
1
,
0
}
,
{
0
,
1
}
,
{
0
,
-
1
}
}
;
34
Queue
*
queue
=
createQueue
(
MAX
*
MAX
)
;
35
enqueue
(
queue
,
(
Point
)
{
sx
,
sy
}
)
;
36
visited
[
sx
]
[
sy
]
=
1
;
37
int
area
=
0
;
38
39
while
(
queue
->
size
)
{
40
Point current
=
dequeue
(
queue
)
;
41
area
++
;
42
43
for
(
int
d
=
0
;
d
<
4
;
d
++
)
{
44
int
newX
=
current
.
x
+
directions
[
d
]
[
0
]
;
45
int
newY
=
current
.
y
+
directions
[
d
]
[
1
]
;
46
if
(
newX
>=
0
&&
newX
<
m
&&
newY
>=
0
&&
newY
<
n
&&
grid
[
newX
]
[
newY
]
==
1
&&
!
visited
[
newX
]
[
newY
]
)
{
47
enqueue
(
queue
,
(
Point
)
{
newX
,
newY
}
)
;
48
visited
[
newX
]
[
newY
]
=
1
;
49
}
50
}
51
}
52
free
(
queue
->
data
)
;
53
free
(
queue
)
;
54
return
area
;
55
}
56
57
int
maxAreaOfIsland
(
int
grid
[
MAX
]
[
MAX
]
,
int
m
,
int
n
)
{
58
int
visited
[
MAX
]
[
MAX
]
=
{
0
}
;
59
int
max_area
=
0
;
60
for
(
int
i
=
0
;
i
<
m
;
i
++
)
{
61
for
(
int
j
=
0
;
j
<
n
;
j
++
)
{
62
if
(
grid
[
i
]
[
j
]
==
1
&&
!
visited
[
i
]
[
j
]
)
{
63
int
area
=
bfs
(
grid
,
visited
,
m
,
n
,
i
,
j
)
;
64
if
(
area
>
max_area
)
{
65
max_area
=
area
;
66
}
67
}
68
}
69
}
70
return
max_area
;
71
}
72
73
int
main
(
)
{
74
int
grid
[
MAX
]
[
MAX
]
=
{
{
0
,
0
,
1
,
0
,
0
,
0
,
0
,
1
,
0
,
0
,
0
,
0
,
0
}
,
75
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
0
,
0
,
0
}
,
76
{
0
,
1
,
1
,
0
,
1
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
}
,
77
{
0
,
1
,
0
,
0
,
1
,
1
,
0
,
0
,
1
,
0
,
1
,
0
,
0
}
,
78
{
0
,
1
,
0
,
0
,
1
,
1
,
0
,
0
,
1
,
1
,
1
,
0
,
0
}
,
79
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
0
,
0
}
,
80
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
1
,
0
,
0
,
0
}
,
81
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
1
,
1
,
0
,
0
,
0
,
0
}
}
;
82
int
m
=
8
,
n
=
13
;
83
printf
(
"Max area of island: %d"
,
maxAreaOfIsland
(
grid
,
m
,
n
)
)
;
84
return
0
;
85
}

Explanation

This BFS C solution uses a queue to iteratively visit all land cells in a discovered island, marking them visited as they are dequeued. Queue-based exploration ensures that contiguous cells are considered in groups.