illuminati

1 min

Spiral Order Matrix II Interviewbit Solution

Updated: Sep 8, 2020

Problem: Spiral Order Matrix II

Problem Description:

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

Input Format:

The first and the only argument contains an integer, A.

Output Format:

Return a 2-d matrix of size A x A satisfying the spiral order.

Constraints:

1 <= A <= 1000

Examples:

Input 1:
 
A = 3
 

 
Output 1:
 
[ [ 1, 2, 3 ],
 
[ 8, 9, 4 ],
 
[ 7, 6, 5 ]
 
]

Input 2:
 
4
 

 
Output 2:
 
[ [1, 2, 3, 4],
 
[12, 13, 14, 5],
 
[11, 16, 15, 6],
 
[10, 9, 8, 7]
 
]

Solution: