Spiral Order Matrix II


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]   ]
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Spiral Order Matrix II on Interview Code Editor
Sign Up
to access hints and editorial solutions for Spiral Order Matrix II

Discussion


Loading...
Click here to start solving coding interview questions