You have to iterate over the given array and track minimum and maximum indices (of both outer array and inner arrays) of the element with a value of 1.
In Java in need to initialize a local variable before you can access it. Therefore, all max variables are initiated with a value of -1 and max variables with a corresponding array length (invalid indices).
public static void main(String[] args) {
int[][] grid = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
int minRow = grid.length;
int minCol = grid[0].length;
int maxRow = -1;
int maxCol = -1;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
if (grid[row][col] == 0) continue; // skipping this element
minRow = Math.min(row, minRow);
minCol = Math.min(col, minCol);
maxRow = Math.max(row, maxRow);
maxCol = Math.max(col, maxCol);
}
}
if (maxRow == -1) {
System.out.println("The given array has no elements with value of 1");
} else {
System.out.println("minRow: " + minRow);
System.out.println("minCol: " + minCol);
System.out.println("maxRow: " + maxRow);
System.out.println("maxCol: " + maxCol);
}
}
Output
minRow: 1
minCol: 4
maxRow: 5
maxCol: 8
Sidenote: from the perspective of object-oriented design, this functionality can be encapsulated in a class, let's say MatrixStatistics. That will allow you to store these data, and might be something like element count, or histogram of frequencies for each subarray inside an object. Which can be returned from a method (as you know, it's not possible to return multiple values from the method), or used as a field. As an example from the JDK of such a mutable object caring statistical data, you might take a look at IntSummaryStatistics class.