This is the Java Program to Find the Row with the Maximum Number of 1’s in a Sorted Matrix.
Given a boolean matrix of 0’s and 1’s, where each row is sorted, print the row first with the maximum number of 1’s.
Example:
Matrix:
0 0 1 1
0 0 1 1
0 1 1 1
0 0 1 1
Output:
0 1 1 1
Iterate through the first row to find the index of the leftmost one in it. For each of the next row, if the element at the index before the leftmost one is zero, then skip that row, otherwise update the index for leftmost one.
Here is the source code of the Java Program to Find the Row with the Maximum Number of 1’s in a Sorted Matrix. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.
//Java Program to Find the Row with the Maximum Number of 1's in a Sorted Matrix
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RowWithMaxOne {
// Function to find and display the row with maximum number of 1's
static void printRow(int[][] matrix) {
int i, j, rownumber = -1;
int leftmost = -1;
for (i = 0; i < matrix[0].length; i++) {
if (matrix[0][i] == 1) {
rownumber = 0;
leftmost = i;
break;
}
}
if (leftmost == -1) {
leftmost = matrix[0].length;
}
for (i = 1; i < matrix[0].length; i++) {
int x = leftmost - 1;
while (x >= 0 && matrix[i][x] == 1) {
rownumber = i;
leftmost = x;
x--;
}
}
if (rownumber != -1) {
System.out.println("Row with maximum number of" +
"One's in Sorted Matrix is");
for (i = 0; i < matrix[rownumber].length; i++) {
System.out.print(matrix[rownumber][i] + " ");
}
} else {
System.out.println("There is no row containing 1's");
}
}
//Main Function to read input
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int rows,columns;
try{
System.out.println("Enter the number of rows");
rows = Integer.parseInt(br.readLine());
System.out.println("Enter the number of columns");
columns = Integer.parseInt(br.readLine());
}
catch(Exception e){
System.out.println("An error occurred");
return;
}
int[][] matrix = new int[rows][columns];
int i,j,prev;
prev=Integer.MIN_VALUE;
System.out.println("Enter the Boolean Matrix");
try{
for(i=0; i<rows; i++){
for(j=0; j<columns; j++){
matrix[i][j] = Integer.parseInt(br.readLine());
}
}
}catch (Exception e){
System.out.println("An error occurred");
return;
}
System.out.println("The matrix is");
for(i=0; i<rows; i++){
for(j=0; j<columns; j++){
System.out.print(matrix[i][j]);
}
System.out.println();
}
if(rows > 0 && columns > 0)
printRow(matrix);
}
}
1. In function printRow(), the loop for (i = 0; i < matrix[0].length; i++), iterates through the first row and finds the index of the leftmost one.
2. If there is no one in the first row, then the variable leftmost is set equal to the number of columns.
3. The loop for (i = 1; i < matrix[0].length; i++) traverses through the remaining rows.
4. The variable x is set to leftmost-1 and the nested loop while (x >= 0 && matrix[i][x] == 1) is used to find the index of the leftmost 1 in the current row.
5. Finally, the row with maximum number of 1’s is displayed.
Time Complexity: O(n+m) where n is the number of rows and m is the number of columns in the matrix respectively.
Case 1 (Positive Test Case): Enter the number of rows 4 Enter the number of columns 4 Enter the Boolean Matrix 1 1 1 1 0 0 0 1 0 0 1 1 0 1 1 1 The matrix is 1111 0001 0011 0111 Row with the maximum number of One's in Sorted Matrix is 1 1 1 1 Case 2 (Positive Test Case - another example): Enter the number of rows 4 Enter the number of columns 4 Enter the Boolean Matrix 0 0 0 1 0 0 1 1 0 1 1 1 0 0 1 1 The matrix is 0001 0011 0111 0011 Row with the maximum number of One's in Sorted Matrix is 0 1 1 1 Case 3 (Negative Test Case): Enter the number of rows 3 Enter the number of columns 3 Enter the Boolean Matrix 0 0 0 0 0 0 0 0 0 The matrix is 000 000 000 There is no row containing 1's
Sanfoundry Global Education & Learning Series – Java Programs.
- Practice Programming MCQs
- Check Java Books
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice BCA MCQs