Algorithms and Data Structures · Middle · Question 30 of 75

How do you count islands (connected groups of 1s) in a grid of 0s and 1s?

Explanation

Each traversal marks one connected component, so the number of traversals started is the answer. Every cell is visited a constant number of times: O(rows × cols).

Further reading

Problem with a question

Algorithms and Data Structures · Middle · question 30

Test yourself

Take the Algorithms and Data Structures · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz