Practice/expertSnake Matrix Pattern
⏱ 00:00
Best: 0/100
Alternating Row TraversalTarget Time: 15 mins

Snake Matrix Pattern

Given an integer N, generate an N x N snake matrix populated from 1 to N² such that even rows (0, 2, 4...) run left-to-right, while odd rows (1, 3, 5...) run right-to-left.

Input Format
A single integer N.
Output Format
Print N rows with numbers separated by space.
Constraints:1 <= N <= 10
Concepts Required:
Boustrophedon traversalConditional reversal per row
Expected Pattern PreviewASCII Monospace
 1  2  3  4
 8  7  6  5
 9 10 11 12
16 15 14 13

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 7 6 5
9 10 11 12
16 15 14 13

Explanation: Row 0 is 1..4, Row 1 is 8..5, Row 2 is 9..12, Row 3 is 16..13.

← Back to All Challenges