Spiral Matrix II Problem

Spiral Matrix II Problem — ExecCode Easy DSA Practice

Solve the Spiral Matrix II problem on ExecCode. Free online easy DSA practice in Arrays - Basics. Write and run code in Java, C++, Python — no signup required to run.

Problem description

Description You are given a positive integer n, generate an n x n matrix filled with elements from 1 to n^2 in spiral order.

Examples

Input {"n": 3}; Output 1 2 3 8 9 4 7 6 5. Input {"n": 1}; Output 1. Input {"n": 4}; Output 1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7

Constraints

1 <= n <= 20

Practice Spiral Matrix II free on ExecCode. Browse DSA problems, topic map, and placement guides.