Construct a grid by placing values so that a special row-and-column condition is satisfied.
Problem
You are given an integer . Build an grid using the values from $0n^2 - 1$ exactly once each.
The grid must be filled so that the arrangement follows a special rule based on the recursive structure of the grid: values are assigned in groups to sub-squares, and each smaller square is filled consistently according to the same rule until the grid is complete.
Your task is to return any valid grid that satisfies the rule.
Notes
- Every number from $0n^2 - 1$ must appear exactly once.
- The grid must be rows by columns.
- If multiple valid grids exist, return any one of them.
Input Format
- An integer
ndescribing the side length of the grid.
Output Format
- Return an
n x ninteger grid containing every value from0ton^2 - 1exactly once.
Constraints
- All values in the output must be integers in the range .
- Each value must appear exactly once.
Example 1
Input
n = 2
Output
[[0,1],[2,3]]
Explanation
This is one valid 2x2 grid using each value exactly once.
Example 2
Input
n = 3
Output
[[0,1,2],[3,4,5],[6,7,8]]
Explanation
This is another valid grid example that uses every number from 0 to 8 exactly once.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.