Count the number of mappings in given binary matrix based on given conditions
Last Updated :
04 Feb, 2022
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>
Time complexity: O(M*N)
Auxiliary Space: O(1)
Similar Reads
Count of moves to escape given Matrix from given position based on given conditions Given an N x M matrix mat[][] where initially we are standing at the cell with index (i, j), the task is to find the number of operations required to escape the given matrix where at each operation mat[x][y] can be reached from mat[i][j] such that x represents the count of 0's in the binary represen
9 min read
Check if there are T number of continuous of blocks of 0s or not in given Binary Matrix Given a binary matrix mat[][] of dimensions M*N, the task is to check whether there exist T continuous blocks of 0s or not and at least 2*max(M, N) cells with value 0s or not. If found to be true, then print Yes. Otherwise, print No. T is defined as the GCD of N and M. A continuous block is defined
13 min read
Number of cells in a matrix that satisfy the given condition Given an N * N grid consisting of empty cells (denoted by '1') and obstacles (denoted by '0'), the task is to find the number of empty cells in which a mirror can be placed to view the east-side view of grid from the south-side. Examples: Input: mat[][] = { {1, 1, 1}, {1, 1, 0}, {1, 0, 1}} Output: 2
11 min read
Minimum number of 1s present in a submatrix of given dimensions in a Binary Matrix Given a 2D binary matrix mat[][] of size N Ã M and two integers A, B, the task is to find the least number of 1s present in a submatrix of dimensions A Ã B or B Ã A. Examples: Input: mat[][] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}} A = 2, B = 1Output: 2Explanation: Any submatrix of size 2 X 1 or 1 X 2 wi
13 min read
Count of numbers in given range L to R which are present in a Matrix Given a matrix(mat[][]), which is sorted row and column-wise in increasing order. Two integers are given, L and R, our task is to count number of elements of the matrix within the range [L, R].Examples: Input: L = 3, R = 11, matrix = {{1, 6, 9} {2, 7, 11} {3, 8, 12}} Output: 6 Explanation: The eleme
8 min read