A depot assigns n parcel ids to m lanes numbered 0 to m-1; a parcel with id x belongs to lane x mod m. Produce the ids sorted primarily by lane number (ascending) and, within the same lane, by ascending id. This is exactly the order you get by concatenating lane 0's sorted ids, then lane 1's, and so on. Output the resulting sequence.
Line 1: two integers n and m.
Line 2: n space-separated non-negative integers, the parcel ids.
n space-separated integers on one line: the ids in the described order.
Example 1
Input
6 3 5 3 9 4 7 6
Expected
3 6 9 4 7 5
Explanation
Remainders mod 3: lane 0 has 3,9,6 -> 3 6 9; lane 1 has 4,7 -> 4 7; lane 2 has 5. Concatenated: 3 6 9 4 7 5.
Example 2
Input
4 1 4 2 3 1
Expected
1 2 3 4
Explanation
With m = 1 every id is in lane 0, so the output is just the ids sorted ascending: 1 2 3 4.
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 →