Data Structure & Algorithms
DSA
Arrays Sliding Window
Spiral Order Matrix 2

Spiral Order Matrix II

Problem Description

Given an integer A, generate a square matrix filled with elements from 1 to A2 in spiral order and return the generated square matrix.

Problem Constraints

1 <= A <= 1000

Input Format

First and only argument is integer A

Output Format

Return a 2-D matrix which consists of the elements added in spiral order.

Example Input

Input 1:
1

Input 2:
2

Example Output

Output 1:
[ [1] ]

Output 2:
[ [1, 2], 
  [4, 3] ]

Example Explanation

Explanation 1:
Only 1 is to be arranged.

Explanation 2:
1 --> 2
      |
      |
4<--- 3

Output

Java
 
Python
 
JavaScript