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

Counting sets of 1s and 0s in a binary matrix can be achieved using nested for loops with conditional statements. A binary matrix is a matrix that consists of only two digits: 1 and 0.

In this article, we will write a JavaScript program for counting the sets of 1s and 0s in a given binary matrix by counting non-empty subsets in each row and column.

Understanding the Formula

For any set with n elements, the total number of non-empty subsets is 2^n - 1.

Let us consider a set {a, b} having 2 elements (n = 2).
Total subsets: 2^2 = 4 ? {}, {a}, {b}, {a,b}
Non-empty subsets: 2^2 - 1 = 3 ? {a}, {b}, {a,b}

Example Walkthrough

<strong>Input:</strong>
arr = [[1, 0, 1], [0, 1, 0]]

Row 1: [1, 0, 1] ? ones = 2, zeroes = 1
Non-empty subsets of 1s: 2^2 - 1 = 3
Non-empty subsets of 0s: 2^1 - 1 = 1
Row 1 total: 3 + 1 = 4

Row 2: [0, 1, 0] ? ones = 1, zeroes = 2  
Row 2 total: (2^1 - 1) + (2^2 - 1) = 1 + 3 = 4

Column 1: [1, 0] ? ones = 1, zeroes = 1
Column 1 total: (2^1 - 1) + (2^1 - 1) = 1 + 1 = 2
Column 2 total: 2, Column 3 total: 2

Sum: 4 + 4 + 2 + 2 + 2 = 14
Remove duplicates: 14 - (2 × 3) = 8

<strong>Output:</strong> 8

Algorithm Steps

  • Initialize a result variable to store the final count
  • For each row, count 1s and 0s, then calculate non-empty subsets using 2^count - 1
  • For each column, count 1s and 0s, then calculate non-empty subsets
  • Subtract rows × columns to remove duplicate counting of individual elements
  • Return the final count

JavaScript Implementation

let arr = [[1, 0, 1], [0, 1, 0]];
let rows = 2;
let cols = 3;

function countBinaryMatrixSets(matrix) {
    let totalSets = 0;
    
    // Count sets in each row
    for (let i = 0; i < rows; i++) {
        let ones = 0, zeroes = 0;
        
        for (let j = 0; j < cols; j++) {
            if (matrix[i][j] === 1) {
                ones++;
            } else {
                zeroes++;
            }
        }
        
        // Add non-empty subsets of 1s and 0s
        totalSets += Math.pow(2, ones) - 1;
        totalSets += Math.pow(2, zeroes) - 1;
    }
    
    // Count sets in each column
    for (let i = 0; i < cols; i++) {
        let ones = 0, zeroes = 0;
        
        for (let j = 0; j < rows; j++) {
            if (matrix[j][i] === 1) {
                ones++;
            } else {
                zeroes++;
            }
        }
        
        // Add non-empty subsets of 1s and 0s
        totalSets += Math.pow(2, ones) - 1;
        totalSets += Math.pow(2, zeroes) - 1;
    }
    
    // Remove duplicates (individual elements counted in both rows and columns)
    return totalSets - (rows * cols);
}

console.log("Binary matrix:", arr);
console.log("Total sets of 1s and 0s:", countBinaryMatrixSets(arr));
Binary matrix: [ [ 1, 0, 1 ], [ 0, 1, 0 ] ]
Total sets of 1s and 0s: 8

Testing with Different Matrix

let matrix2 = [[1, 1], [0, 0]];
let rows2 = 2, cols2 = 2;

function countSets(matrix, r, c) {
    let totalSets = 0;
    
    // Process rows
    for (let i = 0; i < r; i++) {
        let ones = 0, zeroes = 0;
        for (let j = 0; j < c; j++) {
            matrix[i][j] === 1 ? ones++ : zeroes++;
        }
        totalSets += Math.pow(2, ones) - 1 + Math.pow(2, zeroes) - 1;
    }
    
    // Process columns  
    for (let i = 0; i < c; i++) {
        let ones = 0, zeroes = 0;
        for (let j = 0; j < r; j++) {
            matrix[j][i] === 1 ? ones++ : zeroes++;
        }
        totalSets += Math.pow(2, ones) - 1 + Math.pow(2, zeroes) - 1;
    }
    
    return totalSets - (r * c);
}

console.log("Matrix:", matrix2);
console.log("Result:", countSets(matrix2, rows2, cols2));
Matrix: [ [ 1, 1 ], [ 0, 0 ] ]
Result: 6

Time and Space Complexity

Complexity Value Explanation
Time O(m × n) We traverse the matrix twice
Space O(1) Only variables for counting

Conclusion

This algorithm efficiently counts all possible non-empty sets of 1s and 0s in a binary matrix by processing rows and columns separately. The key insight is using the formula 2^n - 1 for non-empty subsets and removing duplicates by subtracting the matrix dimensions.

Updated on: 2026-03-15T23:19:01+05:30

514 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements