Skip to main content

Query And Matrix

 Query And Matrix

Send Feedback

You are given a binary matrix with ‘M’ rows and ‘N’ columns initially consisting of all 0s. 'Q' queries follow. The queries can be of 4 types:

Query 1: 1 R index
Query 2: 1 C index 
Query 3: 2 R index
Query 4: 2 C index

In each query, the first input is the type of the query, the second input is whether we have to consider the row ('R') or the column ('C') and the third input is the index of the row/column. 

For each type 1 query, we need to flip the elements of the row/column having the given index. 

For each type 2 query, we have to output the number of zeros present in the row/column having the given index. 
Note:
Note that the matrix is a binary matrix, meaning that it only contains either 0 or 1.
Example :
Given M = 3, N = 3, 
Queries : 1R1, 1R2, 2C1

So, in the above example the change in the matrix would look like this:

Example

Next query 2C1 will return the count of the number of zeroes in the 1st column: 1
Input Format:
The first line contains an integer ‘T’ which denotes the number of test cases.

The first line of each test case contains two space-separated integers ‘M’ and ‘N’, denoting the dimensions of the matrix.

The next line contains a single integer ‘Q’ denoting the number of queries.

The next ‘Q’ lines of each test contain the queries.
Output Format:
For each test case, return a single integer denoting the number of zeroes according to the given query of type 2.

Print the output of each test case in a separate line.
Note:
You don’t need to print anything; It has already been taken care of. Juts implement the given function.
Constraints:
1 <= T <= 10
1 <= M, N  <= 100
1 <= Q <= 1000
1 <= R <= M
1 <= C <= N
type = 1, 2

Time limit: 1 sec
Sample Input 1:
2
3 3
3
1R1
1R2
2C1
2 2
1
2R1
Sample Output 1:
1
2
Explanation of Sample Output 1:
In test case 1, Next query 2C1 will return the count of the number of zeroes in the 1st column: 1

The change in the matrix after the first and second queries would look like this:

Example

In test case 2, all the matrix elements are zero and hence the count of zeroes will be 2 for the first row.
Sample Input 2:
2
3 3
4
2C1
1R1
1R1
2R1
2 2
5
2C1
1R1
1R1
1R1
2R1
Sample Output 2:
3 3
2 0
Explanation of Sample Output 2:
In test case 1, 

First query 2C1 will return the count of the number of zeroes in the 1st column: 3

Next query 2R1 will return the count of the number of zeroes in the 1st column: 3

The change in the matrix after the second and third queries would look like this:

Example

In test case 2,

First query 2C1 will return the count of the number of zeroes in the 1st column: 2

Next query 2R1 will return the count of the number of zeroes in the 1st column: 0

The change in the matrix after the second and third queries would look like this:

Examplecode for this


public class Solution {
    public static int[] query(int[][] mat, int m, int n, String[] q) {
        int k = 0;
        for (int i = 0; i < q.length; i++) {
            String query = q[i];
            int queryType = query.charAt(0) - '0';

            if (queryType == 2) {
                k++;
            }
        }

        int[] result = new int[k];
        int j = 0;

        for (int i = 0; i < q.length; i++) {
            String query = q[i];
            int queryType = query.charAt(0) - '0';
            char dimension = query.charAt(1);
            int index = query.charAt(2) - '0';

            if (queryType == 1) {
                if (dimension == 'R') {
                    flipRow(mat, index, n);
                } else if (dimension == 'C') {
                    flipColumn(mat, index, m);
                }
            } else if (queryType == 2) {
                int count = 0;
                if (dimension == 'R') {
                    count = countZeroInRow(mat, index, n);
                } else if (dimension == 'C') {
                    count = countZeroInColumn(mat, index, m);
                }
                result[j] = count;
                j++;
            }
        }

        return result;
    }

    private static void flipRow(int[][] matrix, int rowIndex, int n) {
        for (int j = 0; j < n; j++) {
            matrix[rowIndex][j] ^= 1;
        }
    }

    private static void flipColumn(int[][] matrix, int colIndex, int m) {
        for (int i = 0; i < m; i++) {
            matrix[i][colIndex] ^= 1;
        }
    }

    private static int countZeroInRow(int[][] matrix, int rowIndex, int n) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (matrix[rowIndex][j] == 0) {
                count++;
            }
        }
        return count;
    }

    private static int countZeroInColumn(int[][] matrix, int colIndex, int m) {
        int count = 0;
        for (int i = 0; i < m; i++) {
            if (matrix[i][colIndex] == 0) {
                count++;
            }
        }
        return count;
    }
}

Comments

Popular posts from this blog

Code: Form Input Types

  Code: Form Input Types Send Feedback For now, we have created a simple form. But there is a problem that the inputs can take any value other than what they are meant to be. So, we need to change the type of the input element for proper functioning. Your task is to change the type of input according to the meaning of each of them. The meaning of each input element is provided below - 1. Comment - to provide a comment for the blog 2. Name - the name of the person who wants to comment on the blog. 3. Email - to provide the email id of the person. 4. Website - to provide the link to a website that belongs to the above said person. The expected output is - code for this  <!DOCTYPE html> <html>     <head>         <title>Best Coding Practices</title>     </head>     <body>         <div>             <header>       ...

Add View Section to Blog

  Add View Section to Blog Send Feedback Let's add a view section ( using a div tag ) to our blog, which represents the number of visits to our blog. Although it would be static for now, we can change it to dynamic later. So, we will use some text formatting tags to add the views section. The instructions are provided below. Note: The text formatting tags are always inline. Instructions to follow - 1. Add a separate view section (using a div tag ) inside the article in the container (div tag )of the first image of the article. 2. Now, add 2 span's with content as: 'Views:' and '1,137'. Make these span's bold using the appropriate text formatting tag. Expected Blog page is - <!DOCTYPE html> <html>     <head>         <title>Best Coding Practices</title>     </head>     <body>         <div>             <header>       ...

Acer Nitro V ANV15-51 2023 Gaming Laptop: A Comprehensive Review

Acer Nitro V ANV15-51 (2023): A Capable Contender in the Mid-Range Gaming Arena (2000 Words) Acer Nitro V ANV15-51 2023 Gaming Laptop (13th Gen Core i5/ 16GB/ 512GB SSD/ Win11/ 6GB Graph) price in India starts from ₹77,790 It is available at lowest price on Amazon in India as on Apr 24, 2024. Take a look at Acer Nitro V ANV15-51 2023 Gaming Laptop (13th Gen Core i5/ 16GB/ 512GB SSD/ Win11/ 6GB Graph) detailed specifications and features. Acer Nitro V ANV15-51 2023 Gaming Laptop (13th Gen Core i5/ 16GB/ 512GB SSD/ Win11/ 6GB Graph) Quick Specifications Specification Value Display 15.6 inches CPU 13th Gen Intel Core i5 13420H GPU NVIDIA GeForce RTX 4050 RAM 16 GB DDR5 SDRAM Brand Acer Model Name ANV15-51 Screen Size 15.6 Inches Colour Black Hard Disk Size 512 GB CPU Model Core i5 RAM Memory Installed Size 16 GB Operating System Windows 11 Home Special Feature Backlit Keyboard The world of gaming laptops is a constant battleground, with manufacturers vying for dominance with ever-more pow...