In the previous article, we have seen Java Program to Find the Row having Maximum 1’s in a Binary Matrix
In this article we are going to see how we can write a program to count maximum number of zero’s in a binary matrix in JAVA language.
Java Program to Find the Row having Maximum Number of 0 in a Binary Matrix
A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.
Let’s understand it in more simpler way.
| A00 A01 A02 |
Matrix A = | A10 A11 A12 |
| A20 A21 A22 | 3*3
Matrix Arepresents a 3*3 matrix.- ‘
A‘ represents the matrix element - ‘
Aij‘ represents the matrix element at it’s matrix position/index. - ‘
i‘ represents the row index - ‘
j‘ represents the column index - Means
A00=Aijwherei=0andj=0,A01=aijwherei=0andj=1and like this. - Here we have started
rowvalue from 0 andcolumnvalue from 0.
A binary matrix is a matrix that has only 0 or 1 as its elements.
For example: 1 0 0 1 1 1 0 1 0
Let’s see different ways to find the Row having Maximum Number of 0 in a Binary Matrix
Method-1: Java Program to Find the Row having Maximum Number of 0 in a Binary Matrix By Static Initialization of Array Elements
Approach:
- Initialize an binary array of size 3×3, with elements.
- Use two for loops to iterate the rows and columns .
- Inside the for loops count all zeros using a counter.
- Store the count and index if the count is greater than the previous row count.
- Print the result.
Program:
import java.util.Scanner;
public class Matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{0,0,0},{0,1,0},{1,1,0}};
int row, col ;
System.out.print("The matrix elements are:");
printMatrix(arr);
maxOne(arr);
}
// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
// Looks for the highest occurence of 0
static void maxOne(int arr[][])
{
int counter=0,prev[]={0,-1}, row,col;
// Counts and stores the occurence of 0 and the row index
for(row=0;row<3;row++)
{
counter =0;
for(col=0;col<3;col++)
{
if(arr[row][col]==0)
counter++;
}
if(prev[0]<counter)
{
prev[0]=counter;
prev[1]=row;
}
}
if(prev[0]==0)
System.out.println("\nNo rows have 0's");
else
System.out.println("\nThe "+(prev[1]+1)+" row has the highest occurrence of 0: "+prev[0]);
}
}
Output: The matrix elements are: 0 0 0 0 1 0 1 1 0 The 1 row has the highest occurrence of 0 : 3
Method-2: Java Program to Find the Row having Maximum Number of 0 in a Binary Matrix By Dynamic Initialization of Array Elements
Approach:
- Declare one array of size 3×3.
- Ask the user for input of binary array elements and store them in the array using two for loops.
- Use two for loops to iterate the rows and columns .
- Inside the for loops count all zeros in a row using a counter.
- Store the count and index if the count is greater than the previous row count.
- Print the result.
Program:
import java.util.Scanner;
public class matrix
{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Declare the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col ;
// Taking matrix input
System.out.println("Enter matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();
System.out.print("The matrix elements are:");
printMatrix(arr);
maxOne(arr);
}
// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
// Looks for the highet occurence of 0
static void maxOne(int arr[][])
{
int counter=0,prev[]={0,-1}, row,col;
// Counts and stores the occurence of 0 and the row index
for(row=0;row<3;row++)
{
counter =0;
for(col=0;col<3;col++)
{
if(arr[row][col]==0)
counter++;
}
if(prev[0]<counter)
{
prev[0]=counter;
prev[1]=row;
}
}
if(prev[0]==0)
System.out.println("\nNo rows have 1's");
else
System.out.println("\nThe "+(prev[1]+1)+" row has the highest occurrence of 0: "+prev[0]);
}
}
Output: Enter matrix elements : 1 1 1 0 0 1 1 1 0 The matrix elements are: 1 1 1 0 0 1 1 1 0 The 2 row has the highest occurrence of 0: 2
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs: