AlgoDaily

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.

Island of connected ones

Defining "Connected"

Two 1s are considered connected if one is positioned:

  1. Above the other
  2. Below the other
  3. To the left of the other
  4. To the right of the other

Note: Diagonal connections do not count!

Connection Rules

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 0 or 1
  • If n is the number of rows and m is the number of columns, the expected time and space complexity should be O(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 →
Open interactive view →