A data-center technician exports a report of which slots in a server rack are currently occupied. Slot positions are numbered with integers, and the export lists the occupied slot IDs in strictly increasing order with no repeats. For the printed report, any run of slot IDs that are consecutive integers (each exactly one more than the previous) should be collapsed into a single band so the report stays short.
n, the number of occupied slot IDs.n > 0): n space-separated integers, the occupied slot IDs, given in strictly increasing order with no duplicates.Print the occupancy bands as space-separated tokens on a single line, left to right in increasing order. A band that covers a single slot is printed as just that slot's ID. A band that covers two or more consecutive slots a, a+1, ..., b is printed as a->b. If n = 0, print an empty line.
0 <= n <= 10^5-2^31 <= id <= 2^31 - 1).Example 1
Input
6 100 101 102 104 105 107
Expected
100->102 104->105 107
Explanation
Slots 100, 101, 102 are each exactly one more than the previous, so they merge into the band 100->102. Slots 104 and 105 are consecutive, forming 104->105. Slot 107 has no consecutive neighbor in the list, so it stands alone. Printed in order: `100->102 104->105 107`.
Example 2
Input
7 0 2 3 4 6 8 9
Expected
0 2->4 6 8->9
Explanation
Slot 0 is isolated because slot 1 is absent. Slots 2, 3, 4 are consecutive and merge into 2->4. Slot 6 is isolated. Slots 8 and 9 are consecutive and merge into 8->9. Printed in order: `0 2->4 6 8->9`.
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 →