JavaScript Program for Counting sets of 1s and 0s in a binary matrix


A binary matrix is a matrix that consists of only two digits as its name suggests and those two digits are 1 and 0. In this article, we will go through the code with the approach and the proper explanation to understand the concepts in a better way. In this tutorial, we are going to write a JavaScript program for counting the sets of 1s and 0s in a given binary matrix.

Introduction to Problem

In this problem, we are given a binary matrix and we have to find the sets that contain the same value in the row or the column. A set can be of a single value or can be any size but the condition is that it must contain the same element from the row or column. For example,

We are given a binary matrix of two rows and three columns that looks like this −

1 0 1
0 0 1

The number of sets of 1s and 0s can be calculated as

  • Set of size 1 − All the cells of the matrix are distinct sets in itself and there is a total of 6 cells.

  • Set of size 2 − Second row’s first two columns have the same value and the second and third columns have the same value to a total of 2 sets of size 2 and the same values. The first row has two 1s that can be considered as a set also

  • Set of size 3 − Maximum size of the set possible in the matrix is the maximum size of the row and column and here that is 3. But, for size, no set is available that will contain the same values.

Hence we have a total of 6, 3, and 0 sets available for the respective size of 1, 2, and 3. So, finally, our answer will be 9.

Approach

In the previous section, we have seen that we have to focus on only a particular row or column to get the number of similar elements in it. Also, we will use a mathematical concept to count the number of sets with different sizes present in a particular row or column.

For example, if we have a total number of 1s in the row that is x then the total of different sets of 1 with sizes from 1 to x will be from that comes from the Permutations and Combinations.

Let’s move to the approach −

  • First we will get an array that will contain the zeros and ones or that is the given binary matrix.

  • We will create a function to automatically calculate the required answer for any matrix passed to it as the parameter and the answer will be returned as a return value.

  • In the function we will traverse the array or the binary matrix twice, once for the rows and the second time for the columns.

  • In the for loop, we will maintain the two variables one to store the ones and another to store the count of zeroes.

  • After traversing over the single row or column we will update the answer with the formula we have seen above and store the answer in the variable that we have.

  • In the end, we will return the answer by removing the rows*columns from it because we have added them twice.

We have removed the rows * columns because when we are adding the sets with the single value of both zeros and ones once while going with the rows and the second once while going with the columns.

Example

Let’s see the implementation of the above-given steps to get a more understanding of the concepts −

// given variables
var cols = 3; // no of columns
var rows = 2; // no of rows
// function to solve the problem with passed array 
function fun(arr) {
   // variable to store the final answer 
   var ans = 0;
   // traversing over the rows using for loop 
   for (var i = 0; i < rows; i++) {
      var ones = 0, zeroes = 0;
      for ( var j = 0; j < cols; j++) {
         if (arr[i][j] == 1)
         ones++;
         else
         zeroes++;
      }
      // increasing the answer on the basis of current count based on ones 
      ans = ans + Math.pow(2, ones)-1;
      // based on zeros 
      ans = ans + Math.pow(2, zeroes)-1;
   }
   //traversing over the columns using for loop 
   for (var i = 0; i < cols; i++) {
      var ones = 0, zeroes = 0;
      for ( var j = 0; j < rows; j++) {
         if (arr[j][i] == 1)
         ones++;
         else
         zeroes++;
      }
      //increasing the answer on the basis of current count based on ones 
      ans = ans + Math.pow(2, ones)-1;
      // based on zeros 
      ans = ans + Math.pow(2, zeroes)-1;
   }
   return ans - (rows * cols);
}
var arr = [ [ 1, 0, 1 ], [ 0, 1, 0 ] ];
console.log("Total number of the sets in the given binary matrix is: " + fun(arr));

Time and Space Complexity

The time complexity of the above code is O(N*N) where N is the maximum of the number of rows and columns in the given matrix.

No extra space is used so the space complexity of the code is O(1).

Conclusion

In this tutorial, we have learned to write a JavaScript program for counting the sets of 1s and 0s in a given binary matrix. A binary matrix is a matrix that consists of only two digits as its name suggests and those two digits are 1 and 0. The time complexity of the code discussed is O(N*N) and the space complexity if O(1).

Updated on: 24-Mar-2023

189 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements