Count of cells in Binary Matrix having equal bitwise XOR of that row and column
Last Updated :
31 May, 2022
Given a binary matrix arr[][] of N rows and M columns. The task is to find the number of cells in the matrix, where Bitwise XOR of all the elements in its row and column are equal.
Examples:
Input: N = 2, M = 2, arr[][] = [[0, 0], [1, 0]]
Output: 2
Explanation: There are a total of 4 cells.
Out of which only 2 cells satisfy the above condition.
Cell {0, 1} and {1, 0} (indexing is 0 based).
Input: N = 2, M = 2, arr[][] = [[1, 0], [0, 1]]
Output: 4
Explanation: There are a total of 4 cells.
Out of which all of the 4 cells satisfy the condition.
If we take any of the cells then the xor of all the elements
in their row and column is equal to 1.
Approach: The idea to solve the problem using prefix Xor array and greedy approach is as follows:
Pre-compute the xor value of all the elements in each row and column.
Then for each cell (cell (i, j))check whether the xor value of all the elements of that row i and that column j is equal or not.
Follow the below steps to solve this problem:
- Firstly, pre-compute the xor of all the elements of each row and column separately.
- Iterate through each cell of the matrix, let the current cell be (i, j) where i is the row index and j is the column index.
- If the xor of all the elements of row i and column j is equal then increase the count one.
- Return the count.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countCells(vector<vector< int > >& v)
{
int n = v.size(), m = v[0].size();
vector< int > row(n), col(m);
for ( int i = 0; i < n; i++) {
int val = 0;
for ( int j = 0; j < m; j++) {
val ^= v[i][j];
}
row[i] = val;
}
for ( int j = 0; j < m; j++) {
int val = 0;
for ( int i = 0; i < n; i++) {
val ^= v[i][j];
}
col[j] = val;
}
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (row[i] == col[j]) {
ans++;
}
}
}
return ans;
}
int main()
{
vector<vector< int > > arr
= { { 1, 0 }, { 0, 1 } };
cout << countCells(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int countCells( int [][] v)
{
int n = v.length, m = v[ 0 ].length;
int [] row = new int [n];
int [] col = new int [m];
for ( int i = 0 ; i < n; i++) {
int val = 0 ;
for ( int j = 0 ; j < m; j++) {
val ^= v[i][j];
}
row[i] = val;
}
for ( int j = 0 ; j < m; j++) {
int val = 0 ;
for ( int i = 0 ; i < n; i++) {
val ^= v[i][j];
}
col[j] = val;
}
int ans = 0 ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < m; j++) {
if (row[i] == col[j]) {
ans++;
}
}
}
return ans;
}
public static void main(String args[])
{
int [][] arr
= { { 1 , 0 }, { 0 , 1 } };
System.out.println(countCells(arr));
}
}
|
Python3
def countCells(v):
n, m = len (v), len (v[ 0 ])
row, col = [ 0 for _ in range (n)], [ 0 for _ in range (m)]
for i in range ( 0 , n):
val = 0
for j in range ( 0 , m):
val ^ = v[i][j]
row[i] = val
for j in range ( 0 , m):
val = 0
for i in range ( 0 , n):
val ^ = v[i][j]
col[j] = val
ans = 0
for i in range ( 0 , n):
for j in range ( 0 , m):
if (row[i] = = col[j]):
ans + = 1
return ans
if __name__ = = "__main__" :
arr = [[ 1 , 0 ], [ 0 , 1 ]]
print (countCells(arr))
|
C#
using System;
class GFG
{
static int countCells( int [,] v)
{
int n = v.GetLength(0);
int m = 2;
int [] row = new int [n];
int [] col = new int [m];
for ( int i = 0; i < n; i++) {
int val = 0;
for ( int j = 0; j < m; j++) {
val ^= v[i, j];
}
row[i] = val;
}
for ( int j = 0; j < m; j++) {
int val = 0;
for ( int i = 0; i < n; i++) {
val ^= v[i, j];
}
col[j] = val;
}
int ans = 0;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
if (row[i] == col[j]) {
ans++;
}
}
}
return ans;
}
public static void Main()
{
int [,] arr
= { { 1, 0 }, { 0, 1 } };
Console.Write(countCells(arr));
}
}
|
Javascript
<script>
function countCells(v){
let n = v.length,m = v[0].length
let row = new Array(n).fill(0), col = new Array(m).fill(0)
for (let i = 0; i < n; i++)
{
let val = 0
for (let j = 0; j < m; j++)
{
val ^= v[i][j]
}
row[i] = val
}
for (let j = 0; j < m; j++)
{
let val = 0
for (let i = 0; i < n; i++)
{
val ^= v[i][j]
}
col[j] = val
}
ans = 0
for (let i = 0; i < n; i++)
{
for (let j = 0; j < m; j++)
{
if (row[i] == col[j])
ans += 1
}
}
return ans
}
let arr = [[1, 0], [0, 1]]
document.write(countCells(arr), "</br>" )
</script>
|
Time Complexity: O(N*M), as we are using nested loops to traverse N*M times.
Auxiliary Space: O(N+M), as we are using extra space for arrays row and col.
Share your thoughts in the comments
Please Login to comment...