Sobes.tech
Junior — Middle+

Determining the string with the highest number of ones

livecode

Task condition

Given a square matrix containing only zeros and ones. In each row, after the first one appears, all subsequent elements are also equal to one. Find the index of the row with the maximum number of ones using an optimal algorithm.

grid = [
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 1],
    [0, 0, 0, 1, 1],
    [0, 1, 1, 1, 1],
    [0, 0, 0, 0, 1],
    [0, 0, 0, 1, 1]
]