Largest Island

You have been given a non-empty grid consisting of only 0s and 1s. You have to find the maximum area of an island in the given grid.

An island is a group of 1s (representing land) connected horizontally, vertically or diagonally. You can assume that all four edges of the grid are surrounded by 0s (representing water).

Note:
If there is no island present in the grid, then the maximum area is 0.
Input Format:
The first line contains an integer 'T' denoting the number of test cases. 

The first input line of each test case contains two space-separated integers ‘N’ and ‘M’ representing the number of rows and columns of the grid, respectively.

The next 'N' lines represent the rows of the grid. Every row contains 'M' single space-separated integers.
Output Format:
For each test case, return the maximum area of the island in the grid.
Note:
You are not required to print the output, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 100
1 <= N <= 50
1 <= M <= 50
0 <= GRID[i][j] <= 1

Where 'GRID[i][j]' denotes the grid element.

Time limit: 1 sec
CodingNinjas
author
2y
DFS APPROACH

The problem boils down to finding the area of each connected component in the grid, then taking the maximum of them.

If we are on a land cell and explore every cell connected to it 8-dire...read more

CodingNinjas
author
2y
BFS APPROACH

We can also use the breadth-first search technique to explore an island's area.

The ‘bfsArea’ function works as follows:

  1. We will create a queue of integers and put the indices of the curr...read more
Help your peers!
Add answer anonymously...
Paxcom India 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
Get AmbitionBox app

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