A marching band director has a flat roster of member ID numbers, listed in performance order, and wants to arrange them into a rectangular formation with a given number of rows and columns. The formation must be filled row by row: the first row gets the first batch of IDs in order, the second row gets the next batch, and so on, using every member exactly once.
If the requested number of rows times the number of columns does not exactly equal the number of members on the roster, the formation cannot be built at all.
k, the number of members on the roster.k space-separated integers, the member IDs in roster order.r and c, the desired number of rows and columns.If r * c does not equal k, print a single line containing -1.
Otherwise, print r lines, each containing c space-separated member IDs: row i (0-indexed) contains the IDs at roster positions i*c through i*c + c - 1, in order.
Example 1
Input
6 1 2 3 4 5 6 3 2
Expected
1 2 3 4 5 6
Explanation
k=6 and r*c=3*2=6 match, so the formation is possible. Filling row by row with 2 members per row: row 0 = [1,2], row 1 = [3,4], row 2 = [5,6].
Example 2
Input
4 10 20 30 40 2 3
Expected
-1
Explanation
k=4 but r*c=2*3=6, which does not equal k, so the formation cannot be built. Output is -1.
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 →