Number of Islands Problem Statement
You are provided with a 2-dimensional matrix having N
rows and M
columns, containing only 1s (land) and 0s (water). Your goal is to determine the number of islands in this matrix. An island is comprised of connected groups of 1s. A connection between cells exists in all eight possible directions: two vertically, two horizontally, and four diagonally.
Input:
The first line contains two integers N
and M
, representing the number of rows and columns, respectively, in the matrix.
Each of the next N
lines contains M
integers separated by spaces, depicting matrix values.
Output:
The output consists of a single integer which is the number of islands found in the matrix.
Example:
Input:
4 5
1 0 0 1 0
1 0 1 0 0
0 0 0 0 0
1 0 1 0 1
Output:
5
Constraints:
1 <= N <= 10^3
1 <= M <= 10^3
0 <= ARR[i][j] <= 1
- Time limit: 1 second.
Note:
No need for explicit printing; just implement the function and provide the correct output.

AnswerBot
4mo
Count the number of islands in a 2D matrix of 1s and 0s.
Use Depth First Search (DFS) or Breadth First Search (BFS) to traverse the matrix and identify connected groups of 1s.
Maintain a visited array t...read more
Help your peers!
Add answer anonymously...
Tower Research Capital LLC SDE-2 interview questions & answers
A SDE-2 was asked Q. Word Ladder Problem Statement Given two strings, BEGIN and END, along with an ar...read more
A SDE-2 was asked Q. Number of Islands Problem Statement You are provided with a 2-dimensional matrix...read more
A SDE-2 was asked Q. How would you design a system like Splitwise?
Popular interview questions of SDE-2
A SDE-2 was asked Q1. Word Ladder Problem Statement Given two strings, BEGIN and END, along with an ar...read more
A SDE-2 was asked Q2. Number of Islands Problem Statement You are provided with a 2-dimensional matrix...read more
A SDE-2 was asked Q3. How would you design a system like Splitwise?
>
Tower Research Capital LLC SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

