Open In App

Count the number of mappings in given binary matrix based on given conditions

Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary matrix of size M*N containing only 0 and 1. The task is to count the number of mappings in the matrix. There is one mapping between any two 1s if the following conditions are satisfied:

  • The two 1s are located on two different rows: r1 and r2, where r1 < r2.
  • For each row i where r1 < i < r2, there are no 1s in the ith row.

Examples: 

Input: matrix[][] = { {0, 1, 1, 0, 0, 1}, 
                                {0, 0, 0, 0, 0, 0}, 
                                {0, 1, 0, 1, 0, 0}, 
                                {0, 0, 1, 0, 0, 0} };
Output: 8
Explanation: Between each of the following 1s, there is one mapping. In total, there are 8 mappings:
matrix[0][1] -> matrix[2][1]
matrix[0][1] -> matrix[2][3]
matrix[0][2] -> matrix[2][1]
matrix[0][2] -> matrix[2][3]
matrix[0][5] -> matrix[2][1]
matrix[0][5] -> matrix[2][3]
matrix[2][1] -> matrix[3][2]
matrix[2][3] -> matrix[3][2]
Note that there is no mapping between any 1 on the 0th row with any on the 3rd row.
This is because the 2nd row contains 1s, which breaks the second condition.

Input: matrix = { {0, 0, 0}, 
                            {1, 1, 1}, 
                            {0, 0, 0} };
Output: 0
Explanation: There does not exist two 1s located on two different rows.

 

Approach: The idea is to traverse each row and count the number of 1s in it. Traverse the subsequent rows and stop when the first next row contains at least one 1 in it. Compute the number of mappings between these two rows and add to the sum variable. Again repeat this process by making the latest row as starting reference point. Follow the steps below to solve the given problem.

  • Consider the first row as the previous row and count the 1s in it i.e. prev = count of 1s in the first row.
  • Count 1s in subsequent rows and stop when a row has at least one 1 in it i.e. next=count of 1s in the subsequent row.
  • Compute the mappings between the previous and next row i.e. maps = prev * next
  • Add the number of new mappings into sum variable i.e. res += maps.
  • Make the next row as previous and repeat the cycle of finding the next row.
  • Finally, print the number of total mappings.

Below is the implementation of the above approach.

C++




// C++ program for above approach
#include <iostream>
#include <vector>
using namespace std;
 
// Function to count the number of mappings
int CountNumberOfMappings(vector<vector<int> >& matrix)
{
    int i, j, prev = 0, m = matrix.size(),
              n = matrix[0].size();
 
    // Count 1s in first row.
    for (i = 0; i < n; i++) {
        if (matrix[0][i] == 1)
            prev += 1;
    }
 
    // Count 1s in subsequent rows.
    int next = 0, res = 0;
    for (j = 1; j < m; j++) {
        next = 0;
        for (i = 0; i < n; i++) {
            if (matrix[j][i] == 1)
                next += 1;
        }
 
        // Stop when a row has
        // atleast one 1 in it.
        if (next > 0) {
            // Compute number of mappings
            // between prev and next rows.
            res += prev * next;
 
            // Add these mappings to
            // result variable.
            prev = next;
        }
    }
 
    // Return total number of mappings.
    return res;
}
 
// Driver Code
int main()
{
 
    vector<vector<int> > matrix{ { 0, 1, 1, 0, 0, 1 },
                                 { 0, 0, 0, 0, 0, 0 },
                                 { 0, 1, 0, 1, 0, 0 },
                                 { 0, 0, 1, 0, 0, 0 } };
 
    int res = CountNumberOfMappings(matrix);
    cout << res;
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.io.*;
class GFG {
 
  // Function to count the number of mappings
  static int CountNumberOfMappings(int[][] matrix)
  {
    int i, j, prev = 0, m = matrix.length,
    n = matrix[0].length;
 
    // Count 1s in first row.
    for (i = 0; i < n; i++) {
      if (matrix[0][i] == 1)
        prev += 1;
    }
 
    // Count 1s in subsequent rows.
    int next = 0, res = 0;
    for (j = 1; j < m; j++) {
      next = 0;
      for (i = 0; i < n; i++) {
        if (matrix[j][i] == 1)
          next += 1;
      }
 
      // Stop when a row has
      // atleast one 1 in it.
      if (next > 0)
      {
 
        // Compute number of mappings
        // between prev and next rows.
        res += prev * next;
 
        // Add these mappings to
        // result variable.
        prev = next;
      }
    }
 
    // Return total number of mappings.
    return res;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int[][] matrix = { { 0, 1, 1, 0, 0, 1 },
                      { 0, 0, 0, 0, 0, 0 },
                      { 0, 1, 0, 1, 0, 0 },
                      { 0, 0, 1, 0, 0, 0 } };
 
    int res = CountNumberOfMappings(matrix);
    System.out.print(res);
  }
}
 
// This code is contributed by hrithikgarg03188


Python3




# Python code for the above approach
 
# Function to count the number of mappings
def CountNumberOfMappings(matrix):
    prev = 0
    m = len(matrix)
    n = len(matrix[0])
 
    # Count 1s in first row.
    for i in range(n):
        if (matrix[0][i] == 1):
            prev += 1
 
    # Count 1s in subsequent rows.
    next = 0
    res = 0
    for j in range(1, m):
        next = 0
        for i in range(n):
            if (matrix[j][i] == 1):
                next += 1
 
        # Stop when a row has
        # atleast one 1 in it.
        if (next > 0):
            # Compute number of mappings
            # between prev and next rows.
            res += prev * next
 
            # Add these mappings to
            # result variable.
            prev = next
 
    # Return total number of mappings.
    return res
 
# Driver Code
matrix = [[0, 1, 1, 0, 0, 1],
          [0, 0, 0, 0, 0, 0],
          [0, 1, 0, 1, 0, 0],
          [0, 0, 1, 0, 0, 0]]
 
res = CountNumberOfMappings(matrix)
print(res)
 
# This code is contributed by gfgking


C#




// C# program for above approach
using System;
class GFG
{
 
  // Function to count the number of mappings
  static int CountNumberOfMappings(int[, ] matrix)
  {
    int prev = 0;
    int m = matrix.GetLength(0), n
      = matrix.GetLength(1);
 
    // Count 1s in first row.
    for (int i = 0; i < n; i++) {
      if (matrix[0, i] == 1)
        prev += 1;
    }
 
    // Count 1s in subsequent rows.
    int next = 0, res = 0;
    for (int j = 1; j < m; j++) {
      next = 0;
      for (int i = 0; i < n; i++) {
        if (matrix[j, i] == 1)
          next += 1;
      }
 
      // Stop when a row has
      // atleast one 1 in it.
      if (next > 0) {
        // Compute number of mappings
        // between prev and next rows.
        res += prev * next;
 
        // Add these mappings to
        // result variable.
        prev = next;
      }
    }
 
    // Return total number of mappings.
    return res;
  }
 
  // Driver Code
  public static void Main()
  {
 
    int[, ] matrix = { { 0, 1, 1, 0, 0, 1 },
                      { 0, 0, 0, 0, 0, 0 },
                      { 0, 1, 0, 1, 0, 0 },
                      { 0, 0, 1, 0, 0, 0 } };
 
    int res = CountNumberOfMappings(matrix);
    Console.Write(res);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
      // JavaScript code for the above approach
 
 
      // Function to count the number of mappings
      function CountNumberOfMappings(matrix) {
          let i, j, prev = 0, m = matrix.length,
              n = matrix[0].length;
 
          // Count 1s in first row.
          for (i = 0; i < n; i++) {
              if (matrix[0][i] == 1)
                  prev += 1;
          }
 
          // Count 1s in subsequent rows.
          let next = 0, res = 0;
          for (j = 1; j < m; j++) {
              next = 0;
              for (i = 0; i < n; i++) {
                  if (matrix[j][i] == 1)
                      next += 1;
              }
 
              // Stop when a row has
              // atleast one 1 in it.
              if (next > 0) {
                  // Compute number of mappings
                  // between prev and next rows.
                  res += prev * next;
 
                  // Add these mappings to
                  // result variable.
                  prev = next;
              }
          }
 
          // Return total number of mappings.
          return res;
      }
 
      // Driver Code
 
 
      let matrix = [[0, 1, 1, 0, 0, 1],
      [0, 0, 0, 0, 0, 0],
      [0, 1, 0, 1, 0, 0],
      [0, 0, 1, 0, 0, 0]];
 
      let res = CountNumberOfMappings(matrix);
      document.write(res);
 
     // This code is contributed by Potta Lokesh
  </script>


 
 

Output: 

8

 

 

Time complexity: O(M*N)
Auxiliary Space: O(1)

 



Last Updated : 04 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads