Find the row with maximum number of 1s

[Question]: Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s. 

func rowWithMax1s(_ mat: [[Int]]) ->Int {
    
    let row = 4
    let col = 4
    // Initialize first row as row with max 1s
    var maxRowIndex = 0;
    var j = col - 1;
    
    for i in  0..<row {
        // Move left until a 0 is found
        var flag = false
        
        // to check whether a row has more 1's than previous
        while j >= 0 && mat[i][j] == 1 {
            
            j = j - 1; // Update the index of leftmost 1
            // seen so far
            flag = true;//present row has more 1's than previous
        }
        
        // if the present row has more 1's than previous
        if flag {
            maxRowIndex = i; // Update max_row_index
        }
    }
    if maxRowIndex == 0 && mat[0][col - 1] == 0 {
        return -1;
    }
    return maxRowIndex;
}

// Driver Code
let oncesMatric = [[0, 0, 0, 1],
                   [0, 1, 1, 1],
                   [1, 1, 1, 1],
                   [0, 0, 0, 0]];

print("Index of row with maximum 1s is ", rowWithMax1s(oncesMatric))// 2

Leave a Comment

Your email address will not be published. Required fields are marked *