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.

Be the first one to answer
Add answer anonymously...
HCLTech Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter