private static int[][] setZeros(int[][] arr)
boolean[] checked = new boolean[arr.length]; // prevents the row to be doubled checked
int max = Math.max(arr.length, arr[0].length);
int min = Math.min(arr.length, arr[0].length);
for (int i = 0; i < arr.length * arr[0].length; i++) {
int a = i / max;
var b = i % min;
if (arr[a][b] == 0 & !checked[a]) {
arr[a] = new int[arr[0].length];
checked[a] = true;
i += arr[0].length - 1 - a; // jump to the next row
}
}
return arr;
}
For a MxN matrix it will be: