Loading...
Loading...
A garden is planted in a grid of r rows and c columns, each cell holding a plant number. A gardener walks along the outside edge clockwise from the top-left corner, then the next ring inside, and so on until every cell is visited. Print the plant numbers in the order she visits them.
Example 1
Input
3 4 1 2 3 4 5 6 7 8 9 10 11 12
Output
1 2 3 4 8 12 11 10 9 5 6 7
Top row, right column, bottom row backwards, left column up, then the inner ring.
Example 2
Input
3 3 1 2 3 4 5 6 7 8 9
Output
1 2 3 6 9 8 7 4 5
Try the problem first. Open one hint at a time.
Keep four borders: top, bottom, left, right.
Walk the top row and move top down; walk the right column and move right left.
Walk the bottom row only if top <= bottom, and the left column only if left <= right; otherwise a single row or column is printed twice.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor