Clockwise Spiral Matrix Traversal (Rectangular Safe)
Difficulty: Hard · Unit 2: Control Flow, Scope & Recursion · Featured in: Tough Challenge
Given an R x C integer matrix (which may be rectangular, single-row, or single-column), output all elements in clockwise spiral order starting from the top-left corner.
Input & Output Format
Input: Line 1: two integers R and C. Next R lines: C space-separated integers each.
Output: Print all R * C integers on one line separated by single spaces.
Sample 1
3 4 1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 8 12 11 10 9 5 6 7
Peels outer perimeter clockwise, then moves across inner middle row [6, 7].
Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.