Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Semrush - keyword research tool

Linux Foundation Training and Certification

Teach Your Child To Read

Disclosure: My content contains affiliate links.

32,307 questions

42,482 answers

573 users

How to find the row with maximum number of 1’s in sorted rows binary digits matrix with Dart

1 Answer

0 votes
import 'dart:io';

class MyClass
{
    static int FindRowWithMaximumOnes(List<List<int>>matrix) {
        if (matrix.length == 0) {
            return -1;
        }
        var rows = matrix.length;
        var cols = matrix[0].length;
        var row_index = -1;
        var max_count = 0;
        
        for (var  i = 0; i < rows; i++) {
            var count = 0;
            for (var  j = 0; j < cols; j++) {
                if (matrix[i][j] == 1) {
                    count++;
                }
            }
            if (count > max_count) {
                max_count = count;
                row_index = i;
            }
        }
        return row_index;
    }
    
    static void main()
    {
        List<List<int>> matrix = [  [0, 0, 0, 0, 1, 1], 
                                    [0, 0, 0, 1, 1, 1], 
                                    [0, 0, 0, 0, 0, 1], 
                                    [0, 1, 1, 1, 0, 1], 
                                    [0, 0, 0, 1, 1, 1]  ];
                                    
        stdout.write("Row index = " + MyClass.FindRowWithMaximumOnes(matrix).toString());
    }
}

void main(List< String > args){
	MyClass.main();
}




/*
run:

Row index = 3

*/

 



Learn & Practice Python
with the most comprehensive set of 13 hands-on online Python courses
Start now


answered Apr 18, 2023 by avibootz
...