Find Largest Square
MATRIX

Problem

Given a 2D binary matrix matrix, where '1' represents a part of a square and '0' represents an empty space, return the top-left and bottom-right coordinates of the first largest square formed by '1's.

If there are multiple largest squares of the same size, return the coordinates of the first one encountered. If only isolated '1's are present with no square formation, return the coordinates of the first '1' encountered.

Examples

findLargestSquare([ [0, 1, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1] ]); // Returns [1, 0, 2, 1] /* In the first example, the largest 2x2 square of '1' starts: from the second row (index 1), first column (index 0) ends at the third row (index 2), second column (index1). So return [1, 0, 2, 1]. */ findLargestSquare([ [1, 0, 1, 1], [1, 1, 0, 1], [0, 1, 1, 1] ]); // Returns [0, 0, 0, 0] // There is only single 1s so the first 1 appearing in [0,0] is the largest
Loading...
0
1
1
1
1
1
1
1
0
1
1
1