Fill a Special Grid

You are given a non-negative integer n representing a 2^n x 2^n grid. You must fill the grid with integers from 0 to 2^(2n) - 1 to make it special.

A grid is special if it satisfies all the following conditions:

  • All numbers in the top-right quadrant are smaller than those in the bottom-right quadrant.
  • All numbers in the bottom-right quadrant are smaller than those in the bottom-left quadrant.
  • All numbers in the bottom-left quadrant are smaller than those in the top-left quadrant.
  • Each of its quadrants is also a special grid.

Return the special 2^n x 2^n grid.

Note: Any 1 x 1 grid is special.

Example 1
Inputn = 0
Output[[0]]
The only number that can be placed is 0, and there is only one possible position in the grid.
Example 2
Inputn = 1
Output[[3,0],[2,1]]
The quadrants contain top-right 0, bottom-right 1, bottom-left 2, and top-left 3, so 0 < 1 < 2 < 3 satisfies the constraints.

Constraints

  • 0 <= n <= 10

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate