Mobile coding works. A laptop is faster for long sessions.
Problem
Read two non-zero natural numbers `n` and `m` and print a matrix with `n` rows and `m` columns filled with consecutive natural numbers starting from `1`, in a snake pattern as shown in the example.
On rows with an even index (the first, the third, ...) the numbers increase from left to right. On rows with an odd index (the second, the fourth, ...) the numbers increase from right to left, continuing the count.
Input format
Input
input.txt
The program reads from standard input two natural numbers, `n` and `m`, separated by a space. - `1 <= n, m <= 50`
Output format
Output
output.txt
The program prints to standard output the constructed matrix, with values on the same row separated by single spaces, one matrix row per output line.
Example
input
4 5
output
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16
Example
input
4 5
output
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16
Stuck?
Use the Get Hint button in the action bar to reveal a guided hint.
💬 Discussion
Loading...💬
No comments yet. Be the first to start the discussion!
💻 No output yet.
Click Run in the editor to execute your code with the custom input.
Sample cases (from the problem)
Sample Case 1
Input
4 5
Expected output
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16
Sample Case 1
Input
4 5
Expected output
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16
Solve it your way first
Community solutions unlock after you submit a passing solution. Don't peek.
Ready to solve this challenge?
Create a free account to write code, submit solutions, and track your progress.
⌨️ Keyboard Shortcuts
Code Editor
Run Code
Ctrl
Enter
Submit Code
Ctrl
Shift
Enter
Format Code
Shift
Alt
F
Toggle Comment
Ctrl
/
Undo
Ctrl
Z
Redo
Ctrl
Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc