You are given a non-negative integer n representing a 2n x 2n grid. You must fill the grid with integers from 0 to 22n - 1 to make it special. A grid is special if it satisfies all the following conditions:
Return the special 2n x 2n grid.
Note: Any 1x1 grid is special.
Example 1:
Input: n = 0
Output: [[0]]
Explanation:
The only number that can be placed is 0, and there is only one possible position in the grid.
Example 2:
Input: n = 1
Output: [[3,0],[2,1]]
Explanation:
The numbers in each quadrant are:
Since 0 < 1 < 2 < 3, this satisfies the given constraints.
Example 3:
Input: n = 2
Output: [[15,12,3,0],[14,13,2,1],[11,8,7,4],[10,9,6,5]]
Explanation:

The numbers in each quadrant are:
max(3, 0, 2, 1) < min(7, 4, 6, 5)max(7, 4, 6, 5) < min(11, 8, 10, 9)max(11, 8, 10, 9) < min(15, 12, 14, 13)This satisfies the first three requirements. Additionally, each quadrant is also a special grid. Thus, this is a special grid.
Constraints:
0 <= n <= 10Problem Overview: You need to construct a matrix that follows a specific ordering rule across its cells. The grid is built so that values increase according to a structured pattern, and the pattern repeats recursively across sub‑grids. The challenge is recognizing that the grid can be divided into smaller blocks that follow the same ordering rule.
Approach 1: Direct Simulation with Iterative Placement (O(n2) time, O(1) extra space)
The most straightforward idea is to iterate over every cell in the matrix and place values according to the required rule. You compute which region the cell belongs to and assign numbers in the correct order. This works because every position is visited exactly once. However, the logic for determining the correct value per cell can become messy as grid size grows, especially when the ordering rule depends on recursive structure. This approach is useful for understanding the pattern but not ideal for clean implementation.
Approach 2: Divide and Conquer on Matrix Quadrants (O(n2) time, O(log n) recursion space)
The grid structure reveals a repeating pattern across quadrants. Split the matrix into four equal subgrids, solve the same problem for each quadrant, and offset the values based on the quadrant index. Each recursive step fills a smaller n/2 × n/2 matrix, while maintaining the global ordering by adding the correct base value. The recursion stops when the grid reaches the base case (typically 1 × 1). Because each cell is written exactly once, total work remains linear in the number of cells.
This pattern is common in problems involving divide and conquer on a matrix. Instead of computing each cell independently, you build the grid hierarchically. The key insight is that each quadrant contains a contiguous range of values, which lets you reuse the same logic recursively.
Recommended for interviews: The divide and conquer approach is what interviewers typically expect. A brute or simulation solution demonstrates that you understand the pattern, but the recursive quadrant construction shows stronger algorithmic thinking and familiarity with array and matrix decomposition techniques.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n²) | O(1) | Useful for small grids or when implementing the rule directly per cell |
| Divide and Conquer Quadrant Fill | O(n²) | O(log n) | Best approach when the grid pattern repeats recursively across submatrices |
3537. Fill a Special Grid| Contest 448 • Tech Courses • 696 views views
Watch 7 more video solutions →Practice Fill a Special Grid with our built-in code editor and test cases.
Practice on FleetCode