Given a grid of size H*W with all cells initially white. Given N pairs (i, j) in an array, for each pair, paint cell (i, j) with black colour. The task is to determine how many squares of size p×p of the grid contains exactly K black cells, after N cells being painted.
Input: H = 4, W = 5, N = 8, K = 4, p = 3 arr=[ (3, 1), (3, 2), (3, 4), (4, 4), (1, 5), (2, 3), (1, 1), (1, 4) ] Output: 4 Cells the are being painted are shown in the figure below: Here p = 3. There are six subrectangles of size 3*3. Two of them contain three black cells each, and the remaining four contain four black cells each. Input: H = 1, W = 1, N = 1, K = 1, p = 1 arr=[ (1, 1) ] Output: 1
- First thing to observe is that one p*p sub-grid will be different from the other if their starting points are different.
- Second thing is that if the cell is painted black, it will contribute to p^2 different p*p sub-grids.
- For example, suppose cell [i, j] is painted black. Then it will contribute additional +1 to all the subgrids having starting point
[i-p+1][j-p+1] to [i, j].
- Since there can be at most N blacks, for each black cell do p*p iterations and update its contribution for each p*p sub-grids.
- Keep a map to keep track of answer for each cell of the grid.
Below is the implementation of the above approach:
Time Complexity : O(N*p*p)
- Sum of the count of number of adjacent squares in an M X N grid
- Count Magic squares in a grid
- Find an N x N grid whose xor of every row and column is equal
- Find a point that lies inside exactly K given squares
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Possible number of Rectangle and Squares with the given set of elements
- Count number of squares in a rectangle
- Count the total number of squares that can be visited by Bishop in one move
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Find a number which give minimum sum when XOR with every number of array of integers
- Unique paths in a Grid with Obstacles
- Search a Word in a 2D Grid of characters
- Minimum distance to the end of a grid from source
- Count possible moves in the given direction in a grid
- Maximum perimeter of a square in a 2D grid
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.