Spiral Matrix Generator
Medium
matrix
simulation
spiral
Problem Description
Generate an n×n matrix filled with numbers from 1 to n² in spiral order (clockwise from outside to inside).
Input Format
A single integer n (1 ≤ n ≤ 20)
Output Format
Print the n×n matrix with numbers arranged in spiral order, each row on a new line with space-separated values
Constraints
1 ≤ n ≤ 20
Sample Input/Output
Input:
3
Output:
1 2 3 8 9 4 7 6 5
Explanation
Fill the matrix in spiral order: right → down → left → up → repeat
Solution Hints
- Read carefully: Understand input/output format and constraints
- Start simple: Think of brute force solution first, then optimize
- Edge cases: Consider empty inputs, single elements, max constraints
- Data structures: Choose arrays, hash maps, sets based on problem needs
- Time complexity: Ensure your solution fits within time limits
- Test examples: Verify logic with provided sample inputs
200
Points
Points
2000ms
Time Limit
Time Limit
256MB
Memory Limit
Memory Limit
Code Editor
Register to Access Code Editor
Create a free account to solve coding problems and track your progress.
Register Now