Find Number of Islands - The Coding Shala
Home >> Interview Questions >> Find number of Islands
Find Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Number of Islands Java Program
An Island is started if there is 1 present and surrounded by water(0). we will count island if 1 available and make all the adjacent to zero(0) or sink adjacent island so we don't count them again. We can use dfs or bfs to check adjacent lands. The following code is using dfs.
We will bfs to visit the adjacent island.
Here DFS is fast compared to the BFS.
Other Posts You May Like