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 |
2
Time Complexity: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Queries to find the count of connected Non-Empty Cells in a Matrix with updates
- Total number of cells covered in a matrix after D days
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Find safe cells in a matrix
- Find cells in Matrix that are not visited by Robot for given movements
- Find whether there is path between two cells in matrix
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Number of cells in a matrix that satisfy the given condition
- Number of cells in matrix which are equidistant from given two points
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Find the number of consecutive zero at the end after multiplying n numbers
- Print cells with same rectangular sums in a matrix
- Unique cells in a binary matrix
- Shortest distance between two cells in a matrix or grid
- Size of all connected non-empty cells of a Matrix
- Minimum Distance from a given Cell to all other Cells of a Matrix
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01

