Problems > Spiral Order

Spiral Order

Difficulty: 1400 2000 ms 128 MB

implementation

Print the matrix in clockwise spiral order, starting at the top-left corner.

Input: the first line has N and M; the next N lines each have M integers.

Output: all N×M values in spiral order on one line.

Constraints:

  • 1 ≤ N, M ≤ 100
  • −104 ≤ values ≤ 104

Sample Tests

Input 1
3 3
1 2 3
4 5 6
7 8 9
Output 1
1 2 3 6 9 8 7 4 5
Input 2
1 4
1 2 3 4
Output 2
1 2 3 4

Read the Editorial

Discussion (opens after you solve — no spoilers before)

No comments yet.

Submit Your Solution

Log in or create a free account to run and submit code.