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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,887 questions

51,814 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

*/

 



answered Apr 18, 2023 by avibootz
...