public class MyClass {
private static int FindRowWithMaximumOnes(int[][] matrix) {
if (matrix.length == 0) {
return -1;
}
int rows = matrix.length;
int cols = matrix[0].length;
int row_index = -1;
int max_count = 0 ;
for (int i = 0; i < rows; i++) {
int count = 0 ;
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 1) {
count++ ;
}
}
if (count > max_count) {
max_count = count;
row_index = i;
}
}
return row_index;
}
public static void main(String args[]) {
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 } };
System.out.print("Row index = " + FindRowWithMaximumOnes(matrix));
}
}
/*
run:
Row index = 3
*/