AMAZON_HACKON Coding Question – Solved

6 Live
Todd has a grid sheet of paper measuring N by M units and plans to draw squares on it. Each cell in the grid contains an integer X, which represents the center of a square with dimensions (2X - 1) by (2X - 1). Given this grid, Todd wants to know how many squares enclose specific cells. You will be asked Q queries, each with two numbers A and B. Your task is to determine how many of Todd's squares enclose the cell (A, B). Note: Todd's squares can intersect and form additional shapes, but you only need to consider the original squares he draws, not any new shapes formed by intersections. Even if one square entirely overlaps another, both should be considered separately. The coordinates (A, B) are 0-based. grid[i][j] will contain integer values such that Todd's squares are always contained inside the grid. Input Format: The first line contains three integers N, denoting the number of rows in the grid, M, the number of columns in the grid, and Q, the number of queries. The next N lines each contain M integers, representing the grid. The next Q lines each contain two integers: A and B, representing the cell coordinates for the queries. Output Format: For each query, print a single integer representing the number of squares that enclose the given cell (A, B). Constraints: 1 <= N, M <= 10^3 0 <= grid[i][j] <= min(N/2, M/2) 1 <= Q <= 10^5 0 <= A <= N-1 0 <= B <= M-1 Sample Testcase 1: Input: 5 5 1 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 2 0 0 1 0 0 0 2 2 Output: 2 Explanation: Todd's art will look like this: For the query (2, 2), the number of squares enclosing the point is 2 — the red square and the green square. Sample Testcase 2: Input: 3 3 2 0 1 0 0 2 1 0 0 0 0 1 1 1 Output: 2 1 Explanation: Todd's art will look like this: The first query (0, 1) is enclosed by 2 squares, and the second query (1, 1) is enclosed by 1 square.

Asked in: AMAZON_HACKON

Image of the Question

Question Image Question Image Question Image Question Image Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


#include <iostream>
#include <vector>
#include <tuple>
using namespace std;
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To approach this problem effectively, you should start by understanding the nature of the data and what is being asked. The problem provides a 2D grid where each cell contains a number X. This X defines a square centered at that cell with dimensions (2X - 1) x (2X - 1). The side length of the square is always odd, and it extends X - 1 units in each of the four cardinal directions (up, down, left, right) from the center cell.

For each query, you are supposed to count how many of these squares enclose a particular cell (A, B). Since there can be up to 10^5 queries and a grid of size up to 10^3 x 10^3, it would be computationally expensive to evaluate every square for every query directly. Hence, your approach should lean heavily toward preprocessing the grid to make query handling efficient.

The first thing to observe is that each square can be represented as a rectangular region in the grid, from (i - r, j - r) to (i + r, j + r), where (i, j) is the center cell and r = X - 1. Each of these regions can be interpreted as contributing +1 to the count of enclosed squares for each cell inside that region. Instead of iterating over every cell inside each square during each query, it's more effective to preprocess the number of squares covering each cell.

A good way to handle such 2D range additions and point queries is to think in terms of a 2D prefix sum or difference array. The idea is similar to how range updates and queries are optimized in 1D arrays using difference arrays, but extended to 2D. You "mark" the corners of the square in such a way that when you later compute prefix sums, you get the total number of overlapping squares covering each cell.

So, the first stage of your approach should be building a 2D grid (say `coverCount`) of the same size as the input grid, initialized to zero. For every cell in the input grid that has a value greater than 0, determine the square it defines and use a 2D difference array technique to add +1 to the region of the grid it covers. This way, instead of updating possibly thousands of individual cells per square, you perform a constant number of operations (4 updates per square) to encode the square's coverage.

Once all squares have been processed into this difference array, you then compute the 2D prefix sum over this grid. The result is a 2D grid where each cell tells you how many squares enclose it. This entire preprocessing takes roughly O(N*M) time and allows you to answer each query in O(1) time.

From a conceptual point of view, think about the grid as a heat map, where each square adds heat to a specific region. You're simply recording how much heat (square coverage) each cell receives, and then when you're asked about a cell, you're just reading the heat level from that cell.

As you proceed, be cautious about boundary conditions. Since the squares must be entirely within the grid, ensure when calculating (i - r) and (i + r) that these are within [0, N-1], and similarly for columns within [0, M-1]. Only process squares that fit entirely within the grid, as the problem specifies that this is always the case.

In summary, the steps are:
1. For each cell with value > 0, determine the square it defines.
2. Mark the square's region in a 2D difference grid using an efficient rectangle update approach.
3. Compute the prefix sum over the difference grid to get the number of squares covering each cell.
4. For each query, return the value at the queried cell from the prefix sum grid.

This methodology balances preprocessing time and query efficiency. It ensures that you never have to reprocess all the squares per query, which is crucial given the large number of queries possible.
```


Related Questions