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++
#include <iostream>
#include <vector>
using namespace std;
int CountNumberOfMappings(vector<vector< int > >& matrix)
{
int i, j, prev = 0, m = matrix.size(),
n = matrix[0].size();
for (i = 0; i < n; i++) {
if (matrix[0][i] == 1)
prev += 1;
}
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;
}
if (next > 0) {
res += prev * next;
prev = next;
}
}
return res;
}
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
import java.io.*;
class GFG {
static int CountNumberOfMappings( int [][] matrix)
{
int i, j, prev = 0 , m = matrix.length,
n = matrix[ 0 ].length;
for (i = 0 ; i < n; i++) {
if (matrix[ 0 ][i] == 1 )
prev += 1 ;
}
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 ;
}
if (next > 0 )
{
res += prev * next;
prev = next;
}
}
return res;
}
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);
}
}
|
Python3
def CountNumberOfMappings(matrix):
prev = 0
m = len (matrix)
n = len (matrix[ 0 ])
for i in range (n):
if (matrix[ 0 ][i] = = 1 ):
prev + = 1
next = 0
res = 0
for j in range ( 1 , m):
next = 0
for i in range (n):
if (matrix[j][i] = = 1 ):
next + = 1
if ( next > 0 ):
res + = prev * next
prev = next
return res
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)
|
C#
using System;
class GFG
{
static int CountNumberOfMappings( int [, ] matrix)
{
int prev = 0;
int m = matrix.GetLength(0), n
= matrix.GetLength(1);
for ( int i = 0; i < n; i++) {
if (matrix[0, i] == 1)
prev += 1;
}
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;
}
if (next > 0) {
res += prev * next;
prev = next;
}
}
return res;
}
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);
}
}
|
Javascript
<script>
function CountNumberOfMappings(matrix) {
let i, j, prev = 0, m = matrix.length,
n = matrix[0].length;
for (i = 0; i < n; i++) {
if (matrix[0][i] == 1)
prev += 1;
}
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;
}
if (next > 0) {
res += prev * next;
prev = next;
}
}
return res;
}
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);
</script>
|
Time complexity: O(M*N)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
04 Feb, 2022
Like Article
Save Article