A warehouse audit drone patrols a storage grid with m rows and n columns; each cell holds a crate stamped with an integer inventory code. The drone flies the rows in a back-and-forth (boustrophedon) pattern to avoid wasted turns: it passes over row 0 from left to right, row 1 from right to left, row 2 from left to right, and so on, alternating direction on every row, until it has passed over every crate in the grid exactly once, in that order.
To conserve its scanner's battery, the drone does not log a reading for every crate it passes -- it logs a reading only for every other crate, counting continuously across the whole sweep (the count does not reset at the start of a new row). Concretely, if the crates are numbered 0, 1, 2, ... in the order the drone passes over them, the drone logs the code on crates numbered 0, 2, 4, ... and skips the rest.
Given the grid of inventory codes, report the codes that get logged, in the order the drone passes over them.
m and n -- the number of rows and columns.m lines contains n integers -- row i (0-indexed) of the grid, in column order.A single line containing the logged inventory codes, separated by single spaces, in the order the drone passes over them. There is always at least one logged code (the very first crate visited is always logged).
1 <= m, n <= 200-10^5 <= grid[i][j] <= 10^5Example 1
Input
2 3 1 2 3 4 5 6
Expected
1 3 5
Explanation
The drone visits cells in order 1,2,3 (row 0 left-to-right) then 6,5,4 (row 1 right-to-left), giving the continuous visiting order 1,2,3,6,5,4. Logging every other crate starting from the first (positions 0,2,4 of that order) yields 1,3,5.
Example 2
Input
3 2 7 8 9 10 11 12
Expected
7 10 11
Explanation
Row 0 left-to-right gives 7,8; row 1 right-to-left gives 10,9; row 2 left-to-right gives 11,12, so the continuous visiting order is 7,8,10,9,11,12. Logging positions 0,2,4 of that order yields 7,10,11.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →