[Solved] Given a binary matrix of 0 and 1. Find the longest sequence of 1’s either row wise or column wise


var max_1_row = -1;
var row_num = -1
var max_1_col = -1;
var col_num = -1
for (var i = 0; i < rows; i++) {
    var max_1_in_row = 0;
    var max_1_in_row_seq = 0;
    var max_1_in_col = 0;
    var max_1_in_col_seq = 0;
    for (var j = 0; j < cols; j++) {
        if (arr[i][j] == 1) {
            max_1_in_row += arr[i][j];
        } else {
            max_1_in_row = 0;
        }
        if (max_1_in_row > max_1_in_row_seq) {
            max_1_in_row_seq = max_1_in_row;
        }
//By Column
        if (arr[j][i] == 1) {
            max_1_in_col += arr[j][i];
        } else {
            max_1_in_col = 0;
        }
        if (max_1_in_col > max_1_in_col_seq) {
            max_1_in_col_seq = max_1_in_col;
        }
    }
    if (max_1_in_row_seq > max_1_row) {
        max_1_row = max_1_in_row_seq;
        row_num = i + 1;
    }
    if (max_1_in_col_seq > max_1_col) {
        max_1_col = max_1_in_col_seq;
        col_num = i + 1;
    }
}

console.log(“Row:” + max_1_row + ” in row ” + row_num + “\n Column:” + max_1_col + ” in column ” + col_num);

1

solved Given a binary matrix of 0 and 1. Find the longest sequence of 1’s either row wise or column wise