Dsa
Union Find
Easy
Island Count
You are given a 2D grid representing a map of 'land' (1) and 'water' (0). Your task is to count the number of distinct islands. An island is surrounded by water and is formed by connecting adjacent lands (horizontally or vertically). Write a program to calculate the number of distinct islands on the map.
Input:
The first line contains two integers m, n (1 <= m, n <= 100), the dimensions of the grid. The next m lines contain n integers (0 or 1) representing the grid.
Output:
Print the number of distinct islands.
Example:
Input:
3 3
1 1 0
0 1 0
0 0 1
Output:
2
Key concepts
union_findgridcounting
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now