Loading...
Loading...
An exam hall has r rows of c benches, and each bench has one roll number. The invigilator collects answer sheets in a spiral: along the top row from left to right, down the right column, along the bottom row from right to left, up the left column, and then the same again on the inner benches.
Print the roll numbers in the order the sheets are collected.
Example 1
Input
3 3 1 2 3 4 5 6 7 8 9
Output
1 2 3 6 9 8 7 4 5
The outer ring gives 1 2 3 6 9 8 7 4, and the middle bench 5 comes last.
Example 2
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
After the outer ring, the inner part is one row: 6 7.
Try the problem first. Open one hint at a time.
Keep four borders: top, bottom, left and right. They mark the part of the hall not yet collected.
Walk the top row, then move top down by one. Do the same for the right column, the bottom row and the left column.
After the top row and the right column, the remaining part may be empty. Check top <= bottom before the bottom row and left <= right before the left column.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor