Rotate Square Matrix 90 Degrees Clockwise (Zero Aliasing)

Difficulty: Medium · Unit 3: Built-in Data Structures & Hashing · Featured in: CodeTantra Lab Pattern

Given an N x N integer matrix, rotate it 90 degrees clockwise in place (using O(1) extra space beyond the matrix itself) and print the rotated matrix.

Input & Output Format

Input: Line 1: integer N. Next N lines: N space-separated integers.

Output: Print N lines of N space-separated integers.

Sample 1

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

Transpose across main diagonal, then reverse each row.

Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.

Back to Python Notes Hub