On a freight yard's loading dock, boxes arrive one after another on a single conveyor belt in a fixed order, each carrying an integer ID tag. The dock's automated loader always fills exactly k boxes, in the order they arrive, into a rail car before moving on to the next one -- except that the very last rail car may end up with fewer than k boxes if the conveyor runs out. Given the sequence of n box IDs in arrival order and the car capacity k, report the contents of every rail car in loading order.
Print ceil(n / k) lines. The i-th line must list, space-separated and in arrival order, exactly the box IDs loaded into the i-th rail car (the final line may have fewer than k IDs if n is not a multiple of k).
Example 1
Input
7 3 10 20 30 40 50 60 70
Expected
10 20 30 40 50 60 70
Explanation
With capacity k=3, the first three boxes (10, 20, 30) fill car 1, the next three (40, 50, 60) fill car 2, and the single remaining box (70) fills the final, partially-loaded car 3.
Example 2
Input
3 10 5 6 7
Expected
5 6 7
Explanation
Since the capacity k=10 exceeds the total number of boxes n=3, all three boxes (5, 6, 7) fit into a single rail car, so only one line of output is produced.
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 →