Find Column Which is Having Maximum 0’s in a Binary Matrix in Java


In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

A binary matrix is a matrix of 0s and 1s, where each element can only be one of two possible values.

Here we have given a binary matrix which contains set of binary elements and as per the problem statement we have to find the column which is having maximum number of 0’s.

Let’s start!

To show you some instances

Instance-1

Given binary matrix =

1 1 0 0
0 1 0 1
0 0 1 1
1 1 1 1
  • The column(s) with the maximum number of zeros is/are: 1 and 3

Instance-2

Given binary matrix =

0 0 1 1 1
1 0 1 0 0
0 0 0 1 1
0 1 1 1 0
0 1 0 0 1
  • The column(s) with the maximum number of zeros is/are: 1

Instance-3

Given binary matrix =

1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
  • The column(s) with the maximum number of zeros is/are: No such column present

Algorithm

Algorithm-1: (By Using Nested for Loops)

  • Step-1 − The program initializes a binary matrix and two variables to store the maximum zero count and its corresponding column index.

  • Step-2 − It then loops through each column of the matrix and counts the number of zeros in that column.

  • Step-3 − It updates the maximum zero count and its corresponding column index if the current column has more zeros.

  • Step-4 − Finally, it prints the column(s) with the maximum number of zeros or "The given matrix does not contain any zeros" if there are no zeros in the matrix.

Algorithm-2: (By Using Java Streams)

  • Step-1 − The program initializes a binary matrix and two variables to store the maximum zero count and its corresponding column index.

  • Step-2 − It then loops through each column of the matrix and uses a Java Stream to count the number of zeros in that column.

  • Step-3 − It updates the maximum zero count and its corresponding column index if the current column has more zeros.

  • Step-4 − Finally, it prints the column(s) with the maximum number of zeros or "The given matrix does not contain any zeros" if there are no zeros in the matrix.

  • Step-5 − Java Streams provide a functional programming approach for processing data and can significantly reduce the code required to perform complex operations on collections of data.

Syntax

The Matrix.length() method in Java returns the length of the given matrix.

Below refers to the syntax of it −

inputMatrix.lenght

where, ‘inputMatrix’ refers to the given matrix.

Arrays.stream(matrix) converts the two-dimensional integer array matrix into a stream of arrays, where each array in the stream represents a row in the matrix.

Arrays.stream(matrix)

.mapToInt(row -> row[column]) maps each array (row) in the stream to an integer value corresponding to the element at the specified column index.

.mapToInt(row -> row[column]) 

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Nested for Loops

  • By Using Java Stream

Let’s see the program along with its output one by one.

Approach-1: By Using Nested for Loop

In this approach, binary matrix elements will be initialized in the program. Then calls a user defined method by passing the matrix as parameter and inside method as per the algorithm-1 using nested for loop calculate the column which is having maximum number of 0’s in the given binary matrix.

Example

public class Main {
   public static void main(String[] args) {
      
      // Create a binary matrix
      int[][] matrix = {{1, 0, 1, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}, {0, 1, 1, 0}};
      
      // Initialize variables to store the maximum zero count and its corresponding column index
      int maxZeroCount = -1;
      int maxZeroColumn = -1;
      
      // Loop through each column of the matrix and count the number of zeros in that column
      for (int j = 0; j < matrix[0].length; j++) {
         int zeroCount = 0;
         for (int[] row : matrix) {
            if (row[j] == 0) {
               zeroCount++;
            }
         }
         
         // Update the maximum zero count and its corresponding column index if the current column has more zeros
         if (zeroCount > maxZeroCount) {
            maxZeroCount = zeroCount;
            maxZeroColumn = j;
         }
      }
      
      // Print the result
      if (maxZeroCount == 0) {
         System.out.println("The given matrix does not contain any zeros.");
      } else {
         System.out.print("The column(s) with the maximum number of zeros is/are: ");
         for (int j = 0; j < matrix[0].length; j++) {
            int zeroCount = 0;
            for (int[] row : matrix) {
               if (row[j] == 0) {
                  zeroCount++;
               }
            }
            if (zeroCount == maxZeroCount) {
               System.out.print(j + " ");
            }
         }
      }
   }
}

Output

The column(s) with the maximum number of zeros is/are: 3

Approach-2: By Using Java Stream

In this approach, binary matrix elements will be initialized in the program. Then calls a user defined method by passing the matrix as parameter and inside method as per the algorithm-2 using java streams calculate the column which is having maximum number of 0’s in the given binary matrix.

Example

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      
      // Create a binary matrix
      int[][] matrix = {{1, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 0, 0}, {0, 1, 1, 0}};
      
      // Initialize variables to store the maximum zero count and its corresponding column index
      int maxZeroCount = -1;
      int maxZeroColumn = -1;
      
      // Loop through each column of the matrix and count the number of zeros in that column using a lambda expression
      for (int j = 0; j < matrix[0].length; j++) {
         final int column = j; // Make a copy of j to use inside the lambda expression
         int zeroCount = (int) Arrays.stream(matrix)
               .mapToInt(row -> row[column])
               .filter(value -> value == 0)
               .count();
      
         // Update the maximum zero count and its corresponding column index if the current column has more zeros
         if (zeroCount > maxZeroCount) {
            maxZeroCount = zeroCount;
            maxZeroColumn = j;
         }
      }
      
      // Print the result
      if (maxZeroCount == 0) {
         System.out.println("The given matrix does not contain any zeros.");
      } else {
         System.out.print("The column(s) with the maximum number of zeros is/are: ");
         for (int j = 0; j < matrix[0].length; j++) {
            final int column = j; // Make a copy of j to use inside the lambda expression
            int zeroCount = (int) Arrays.stream(matrix)
               .mapToInt(row -> row[column])
               .filter(value -> value == 0)
               .count();
            if (zeroCount == maxZeroCount) {
               System.out.print(j + " ");
            }
         }
      }
   }
}

Output

The column(s) with the maximum number of zeros is/are: 2 3

In this article, we explored different approaches to find the column which is having maximum number of 0’s in a binary matrix by using Java programming language.

Updated on: 04-May-2023

192 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements