insidcode
ProblemsDuelLeaderboardStats
Sign In
  1. Home
  2. Problems
  3. Print elements of an M \times N matrix in Zig-Zag / Snake pattern (alternating rows left-to-right and right-to-left).

Print elements of an M \times N matrix in Zig-Zag / Snake pattern (alternating rows left-to-right and right-to-left).

Difficulty: Hard

Phase: 6 · Mixed Logical Challenges

XP Reward: 30 XP

Topics: Mixed Logical Challenges, Hard

Supported Languages: Python, JavaScript, C, C++, Java

Problem Description

Given R and C followed by R x C matrix, print elements in Snake / Zig-Zag traversal (row 0 left-to-right, row 1 right-to-left, etc.). Input Format: Read input from standard input (stdin). Output Format: Print the result to standard output (stdout).

Constraints

  • -10^9 <= N <= 10^9

Examples

Example 1

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

Output: 1 2 3 6 5 4 7 8 9

Explanation: Sample execution for input 3 3 1 2 3 4 5 6 7 8 9