Most Strongly Connected
Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.
Description
Challenge: Finding the Largest Island of Connected Ones in a Matrix
Introduction
Imagine you're an explorer on a digital ocean filled with islands made up of ones (1s) and water represented by zeroes (0s). Your task is to find the largest island composed of connected 1s in a given 2D grid (or matrix).
Here's a sample grid:
const matrix = [
[1, 1, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 1, 0, 1, 0],
[1, 0, 0, 0, 0]
];
Your Task
Write a class or method called MostConnectedOnes(matrix: array[]) that identifies the largest island of connected 1s in this digital ocean.

Defining "Connected"
Two 1s are considered connected if one is positioned:
- Above the other
- Below the other
- To the left of the other
- To the right of the other
Note: Diagonal connections do not count!

Examples
In the very first sample grid, the largest island has a size of 5. The connected 1s can be found starting at [0][0], moving right to [0][1], down to [1][1], right to [1][2], and finally down again to [2][1].
const matrix = [
[1, 1, 0, 0, 0],
[0, 1, 1, 0, 0],
[0, 1, 0, 1, 0],
[1, 0, 0, 0, 0]
];
Here's another example:
const matrix = [
[1, 1, 0, 0],
[0, 0, 1, 0],
[0, 1, 1, 0],
[1, 0, 0, 0]
];
const mco = new MostConnectedOnes(matrix);
mco.max();
// Output: 3
Constraints
- The total number of elements in the matrix will be <=
100000 - The value of each element inside the matrix will either be
0or1 - If
nis the number of rows andmis the number of columns, the expected time and space complexity should beO(n * m)
Your task is to find the "area" or the size of the largest island of connected 1s in the given matrix.
```js
class MostConnectedOnes {
constructor(matrix) {
// implement this method
}
// main interface
max() {
// implement this method
}
}
```
CONTINUE WITH ALGODAILY PREMIUM
Keep going with the complete tutorial.
Unlock the remaining explanations and exercises, alongside the full learning library.
Explore Premium →