Given a boolean matrix mat[][] of size M * N, the task is to print the count of indices from the matrix whose corresponding row and column contain an equal number of set bits.
Examples:
Input; mat[][] = {{0, 1}, {1, 1}}
Output: 2
Explanation:
Position (0, 0) contains 1 set bit in corresponding row {(0, 0), (0, 1)} and column {(0, 0), (1, 0)}.
Position (1, 1) contains 2 set bits in corresponding row {(1, 0), (1, 1)} and column {(0, 1), (1, 1)}.
Input: mat[][] = {{0, 1, 1, 0}, {0, 1, 0, 0}, {1, 0, 1, 0}}
Output: 5
Naive Approach: The simplest approach to solve is to count and store the number of set bits present in the elements of each row and column of the given matrix. Then, traverse the matrix and for each position, check if the count of set bits of both the row and column is equal or not. For every index for which the above condition is found to be true, increment count. Print the final value of count after complete traversal of the matrix.
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient Approach: To optimize the above approach, follow the steps below:
- Initialize two temporary arrays row[] and col[] of sizes N and M respectively.
- Traverse the matrix mat[][]. Check for every index (i, j), if mat[i][j] is equal to 1 or not. If found to be true, increment row[i] and col[j] by 1.
- Traverse the matrix mat[][] again. Check for every index (i, j), if row[i] and col[j] are equal or not. If found to be true, increment count.
- Print the final value of count.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int countPosition(vector<vector< int >> mat)
{
int n = mat.size();
int m = mat[0].size();
vector< int > row(n);
vector< int > col(m);
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (mat[i][j] == 1)
{
col[j]++;
row[i]++;
}
}
}
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (row[i] == col[j])
{
count++;
}
}
}
return count;
}
int main()
{
vector<vector< int >> mat = { { 0, 1 },
{ 1, 1 } };
cout << (countPosition(mat));
}
|
Java
import java.util.*;
import java.lang.*;
class GFG {
static int countPosition( int [][] mat)
{
int n = mat.length;
int m = mat[ 0 ].length;
int [] row = new int [n];
int [] col = new int [m];
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if (mat[i][j] == 1 ) {
col[j]++;
row[i]++;
}
}
}
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if (row[i] == col[j]) {
count++;
}
}
}
return count;
}
public static void main(
String[] args)
{
int mat[][] = { { 0 , 1 },
{ 1 , 1 } };
System.out.println(
countPosition(mat));
}
}
|
Python3
def countPosition(mat):
n = len (mat)
m = len (mat[ 0 ])
row = [ 0 ] * n
col = [ 0 ] * m
for i in range (n):
for j in range (m):
if (mat[i][j] = = 1 ):
col[j] + = 1
row[i] + = 1
count = 0
for i in range (n):
for j in range (m):
if (row[i] = = col[j]):
count + = 1
return count
mat = [ [ 0 , 1 ],
[ 1 , 1 ] ]
print (countPosition(mat))
|
C#
using System;
class GFG{
static int countPosition( int [,] mat)
{
int n = mat.GetLength(0);
int m = mat.GetLength(1);
int [] row = new int [n];
int [] col = new int [m];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (mat[i, j] == 1)
{
col[j]++;
row[i]++;
}
}
}
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < m; j++)
{
if (row[i] == col[j])
{
count++;
}
}
}
return count;
}
public static void Main(String[] args)
{
int [,]mat = {{0, 1},
{1, 1}};
Console.WriteLine(countPosition(mat));
}
}
|
Javascript
<script>
function countPosition(mat)
{
var n = mat.length;
var m = mat[0].length;
var row = Array.from({length: n}, (_, i) => 0);
var col = Array.from({length: m}, (_, i) => 0);
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (mat[i][j] == 1)
{
col[j]++;
row[i]++;
}
}
}
var count = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (row[i] == col[j])
{
count++;
}
}
}
return count;
}
var mat = [ [ 0, 1 ],
[ 1, 1 ] ];
document.write(countPosition(mat));
</script>
|
Time Complexity: O(N * M)
Auxiliary Space: O(N+M)
Approach#2:using brute force
Algorithm
1. Initialize count to zero
2. Iterate over each cell (i, j) in the matrix
3.Initialize row_sum and col_sum to zero
4. Iterate over each cell in the ith row and jth column
a. If the cell is set, increment row_sum and col_sum
5.If row_sum is equal to col_sum, increment count
6. Return count
C++
#include <iostream>
#include <vector>
int GFG(std::vector<std::vector< int >>& matrix) {
int count = 0;
for ( int i = 0; i < matrix.size(); ++i) {
for ( int j = 0; j < matrix[0].size(); ++j) {
int row_sum = 0;
int col_sum = 0;
for ( int k = 0; k < matrix.size(); ++k) {
row_sum += matrix[i][k];
col_sum += matrix[k][j];
}
if (row_sum == col_sum) {
count += 1;
}
}
}
return count;
}
int main() {
std::vector<std::vector< int >> matrix = {{0, 1}, {1, 1}};
std::cout <<GFG(matrix) << std::endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
public class Main {
public static int GFG( int [][] matrix) {
int count = 0 ;
int rows = matrix.length;
int cols = matrix[ 0 ].length;
for ( int i = 0 ; i < rows; ++i) {
for ( int j = 0 ; j < cols; ++j) {
int rowSum = 0 ;
int colSum = 0 ;
for ( int k = 0 ; k < rows; ++k) {
rowSum += matrix[i][k];
colSum += matrix[k][j];
}
if (rowSum == colSum) {
count += 1 ;
}
}
}
return count;
}
public static void main(String[] args) {
int [][] matrix = {{ 0 , 1 }, { 1 , 1 }};
int result = GFG(matrix);
System.out.println(result);
}
}
|
Python3
def count_positions(matrix):
count = 0
for i in range ( len (matrix)):
for j in range ( len (matrix[ 0 ])):
row_sum = 0
col_sum = 0
for k in range ( len (matrix)):
row_sum + = matrix[i][k]
col_sum + = matrix[k][j]
if row_sum = = col_sum:
count + = 1
return count
matrix = [ [ 0 , 1 ],
[ 1 , 1 ] ]
print ( count_positions(matrix))
|
Javascript
function GFG(matrix) {
let count = 0;
const rows = matrix.length;
const cols = matrix[0].length;
for (let i = 0; i < rows; ++i) {
for (let j = 0; j < cols; ++j) {
let rowSum = 0;
let colSum = 0;
for (let k = 0; k < rows; ++k) {
rowSum += matrix[i][k];
colSum += matrix[k][j];
}
if (rowSum === colSum) {
count += 1;
}
}
}
return count;
}
const matrix = [[0, 1], [1, 1]];
const result = GFG(matrix);
console.log(result);
|
Time Complexity: O(n^3), where n is the size of the matrix
Space Complexity: O(1)