Practice/expertMatrix Border Traversal
⏱ 00:00
Best: 0/100
Layered Ring UnwrappingTarget Time: 20 mins

Matrix Border Traversal

Given an integer N, generate an N x N matrix numbered 1 to N² row-wise. Then print its boundary elements in clockwise order: top row, right column, bottom row (reverse), left column (reverse).

Input Format
A single integer N (N >= 2).
Output Format
Print the boundary numbers in a single line separated by space.
Constraints:2 <= N <= 10
Concepts Required:
Clockwise perimeter traversal2D grid indexingAvoiding duplicate corner printing
Expected Pattern PreviewASCII Monospace
Matrix:
 1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 16

Boundary Traversal:
1 2 3 4 8 12 16 15 14 13 9 5

Notice how character spacing and row counts scale systematically based on the input N.

Concrete Examples

Example 1
Input:
4
Output:
1 2 3 4 8 12 16 15 14 13 9 5

Explanation: Top: 1 2 3 4 -> Right: 8 12 16 -> Bottom: 15 14 13 -> Left: 9 5.

← Back to All Challenges