Find Number of Even cells in a Zero Matrix after Q queries
Last Updated :
08 Jun, 2021
Given a Zero matrix of N x N size, the task is to find the numbers of cells containing even numbers after performing Q queries. Each query will be in the form of (X, Y) such that for a given cell (X, Y), an increment operation has to be performed on all cells in the X'th row and Y'th column.
Note: The initial 0's are also to be taken as even numbers in the count.
Examples:
Input: N = 2, Q = { {1, 1}, {1, 2}, {2, 1} }
Output: 2
Explanation:
In the first query, we add 1
to all elements of row 1 and column 1.
Our matrix become
2 1
1 0
In the second query, we add 1
to all elements of row 1 and column 2.
Our matrix become
3 3
1 1
In the last query, we add 1
to all elements of row 2 and column 1.
Our matrix finally become
4 3
3 2
In the final updated matrix, there
are 2 even numbers 4 and 3
respectively, hence ans is 2
Input: N = 2, Q = { {1, 1} }
Output: 1
Naive Approach: The Naive approach would be to update each and every cell in the matrix as per the query. Then traverse the matrix to find out the even cells.
Time Complexity: O(N2)
Efficient Approach:
- Maintain two arrays, one for rows operation and one for column operation.
- The row[i] will store the number of operations on i+1th row and col[i] will store the number of operations on i+1th column.
- If 1 is to be added to ith row, we will do row[i-1]++, assuming 0 based indexing.
- Same for columns.
- Now, we need to observe that odd + odd = even and even + even = even.
- Hence if row[i] and col[j] are both odd or even, then that cell will have even value.
- So we need to just count odd and even values in both arrays and multiply them.
Below is the implementation of above approach:
C++
// C++ program find Number of Even cells
// in a Zero Matrix after Q queries
#include <bits/stdc++.h>
using namespace std;
// Function to find the number of
// even cell in a 2D matrix
int findNumberOfEvenCells(int n, int q[][2], int size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
int row[n] = { 0 };
int col[n] = { 0 };
for (int i = 0; i < size; i++) {
int x = q[i][0];
int y = q[i][1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
int r1 = 0, r2 = 0;
int c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (int i = 0; i < n; i++) {
// Count of rows having even numbers
if (row[i] % 2 == 0) {
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1) {
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0) {
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1) {
c2++;
}
}
int count = r1 * c1 + r2 * c2;
return count;
}
// Driver code
int main()
{
int n = 2;
int q[][2] = { { 1, 1 },
{ 1, 2 },
{ 2, 1 } };
int size = sizeof(q) / sizeof(q[0]);
cout << findNumberOfEvenCells(n, q, size);
return 0;
}
Java
// Java program find Number of Even cells
// in a Zero Matrix after Q queries
class GFG
{
// Function to find the number of
// even cell in a 2D matrix
static int findNumberOfEvenCells(int n, int q[][], int size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
int row[] = new int[n] ;
int col[] = new int[n] ;
for (int i = 0; i < size; i++)
{
int x = q[i][0];
int y = q[i][1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
int r1 = 0, r2 = 0;
int c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (int i = 0; i < n; i++)
{
// Count of rows having even numbers
if (row[i] % 2 == 0)
{
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1)
{
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0)
{
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1)
{
c2++;
}
}
int count = r1 * c1 + r2 * c2;
return count;
}
// Driver code
public static void main (String[] args)
{
int n = 2;
int q[][] = { { 1, 1 },
{ 1, 2 },
{ 2, 1 } };
int size = q.length;
System.out.println(findNumberOfEvenCells(n, q, size));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program find Number of Even cells
# in a Zero Matrix after Q queries
# Function to find the number of
# even cell in a 2D matrix
def findNumberOfEvenCells(n, q, size) :
# Maintain two arrays, one for rows operation
# and one for column operation
row = [0]*n ;
col = [0]*n
for i in range(size) :
x = q[i][0];
y = q[i][1];
# Increment operation on row[i]
row[x - 1] += 1;
# Increment operation on col[i]
col[y - 1] += 1;
r1 = 0;
r2 = 0;
c1 = 0;
c2 = 0;
# Count odd and even values in
# both arrays and multiply them
for i in range(n) :
# Count of rows having even numbers
if (row[i] % 2 == 0) :
r1 += 1;
# Count of rows having odd numbers
if (row[i] % 2 == 1) :
r2 += 1;
# Count of columns having even numbers
if (col[i] % 2 == 0) :
c1 +=1;
# Count of columns having odd numbers
if (col[i] % 2 == 1) :
c2 += 1;
count = r1 * c1 + r2 * c2;
return count;
# Driver code
if __name__ == "__main__" :
n = 2;
q = [ [ 1, 1 ],
[ 1, 2 ],
[ 2, 1 ] ];
size = len(q);
print(findNumberOfEvenCells(n, q, size));
# This code is contributed by AnkitRai01
C#
// C# program find Number of Even cells
// in a Zero Matrix after Q queries
using System;
class GFG
{
// Function to find the number of
// even cell in a 2D matrix
static int findNumberOfEvenCells(int n, int [,]q, int size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
int []row = new int[n] ;
int []col = new int[n] ;
for (int i = 0; i < size; i++)
{
int x = q[i, 0];
int y = q[i, 1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
int r1 = 0, r2 = 0;
int c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (int i = 0; i < n; i++)
{
// Count of rows having even numbers
if (row[i] % 2 == 0)
{
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1)
{
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0)
{
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1)
{
c2++;
}
}
int count = r1 * c1 + r2 * c2;
return count;
}
// Driver code
public static void Main ()
{
int n = 2;
int [,]q = { { 1, 1 },
{ 1, 2 },
{ 2, 1 } };
int size = q.GetLength(0);
Console.WriteLine(findNumberOfEvenCells(n, q, size));
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// JavaScript program find Number of Even cells
// in a Zero Matrix after Q queries
// Function to find the number of
// even cell in a 2D matrix
function findNumberOfEvenCells(n, q, size)
{
// Maintain two arrays, one for rows operation
// and one for column operation
let row = new Array(n);
row.fill(0);
let col = new Array(n);
col.fill(0);
for (let i = 0; i < size; i++)
{
let x = q[i][0];
let y = q[i][1];
// Increment operation on row[i]
row[x - 1]++;
// Increment operation on col[i]
col[y - 1]++;
}
let r1 = 0, r2 = 0;
let c1 = 0, c2 = 0;
// Count odd and even values in
// both arrays and multiply them
for (let i = 0; i < n; i++)
{
// Count of rows having even numbers
if (row[i] % 2 == 0)
{
r1++;
}
// Count of rows having odd numbers
if (row[i] % 2 == 1)
{
r2++;
}
// Count of columns having even numbers
if (col[i] % 2 == 0)
{
c1++;
}
// Count of columns having odd numbers
if (col[i] % 2 == 1)
{
c2++;
}
}
let count = r1 * c1 + r2 * c2;
return count;
}
let n = 2;
let q = [ [ 1, 1 ],
[ 1, 2 ],
[ 2, 1 ] ];
let size = q.length;
document.write(findNumberOfEvenCells(n, q, size));
</script>
Time Complexity: O(N)
Similar Reads
Queries to find number of connected grid components of given sizes in a Matrix Given a matrix mat[][] containing only of 0s and 1s, and an array queries[], the task is for each query, say k, is to find the number of connected grid components (cells consisting of 1s) of size k. Note: Two cells are connected if they share an edge in the direction up, down, left, and right not di
14 min read
Number of Islands after changing given cell for K queries Given two integers N and M which denote the size of a matrix where initially all the cells are 0s. There are also K queries each of type {x, y} that denotes a cell of the matrix whose value needs to be flipped from 0 to 1. The task is to find the number of islands of 1s after each query. Note: An is
10 min read
Count number of free cell present in the Matrix Given a Matrix size N*N and an integer K. Initially, the matrix contains only 0. You are given K tasks and for each task, you are given two coordinates (r, c). Where coordinates (r, c) denotes the rth row and the cth column of the given matrix. You have to perform each task sequentially in the given
11 min read
Range Queries for finding the Sum of all even parity numbers Given Q queries where each query consists of two numbers L and R which denotes a range [L, R]. The task is to find the sum of all Even Parity Numbers lying in the given range [L, R]. Parity of a number refers to whether it contains an odd or even number of 1-bits. The number has Even Parity if it co
10 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
Queries to check if a path made up of even numbers from source to destination exists in a Matrix Given two arrays R[] and C[] consisting of N integers such that a matrix of dimensions N x N can be formed whose element at index (i, j) is given by (R[i] + C[j]) for all possible pairs from the two arrays. Given a matrix Queries[][] with each row representing a query of the type {A, B, X, Y}, the t
13 min read
Queries to find the count of connected Non-Empty Cells in a Matrix with updates Given a boolean matrix mat[][] consisting of N rows and M columns, initially filled with 0's(empty cells), an integer K and queries Q[][] of the type {X, Y}, the task is to replace mat[X][Y] = 1(non-empty cells) and count the number of connected non-empty cells from the given matrix.Examples: Input:
15+ min read
Queries on number of Binary sub-matrices of Given size Given a Binary Matrix of size N X M, the task is to answer Q queries of the following type; Query(a, b): Find the number of sub matrices of size a X a with each of its element consisting of the Binary Number b. We basically need to find submatrices of given sizes with all 1s or all 0s. Examples: Inp
12 min read
Frequencies of even and odd numbers in a matrix Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
9 min read
Final Matrix after incrementing all cells of given Submatrices Given a matrix of dimension N*N whose all cells are initially 0. You are given Q queries, each of type {a, b, c, d} where (a, b) is the top-left cell and (c, d) is the bottom-right cell of a submatrix whose every cell needs to be incremented by 1. The task is to find the final matrix after increment
11 min read