A loading dock scans pallets one by one as they roll off an incoming truck. Each pallet carries a unique serial number, and the scanner has already sorted these numbers into strictly increasing order before handing them to you. The dock crew groups pallets into storage blocks: a block is a maximal run of scanned serial numbers in which each number is exactly one greater than the number before it (that is, the numbers in the run are consecutive integers with no gaps). Given the sorted serial numbers, report every storage block, in the order the numbers appear, as its starting and ending serial number.
n — the number of pallets scanned.n space-separated integers id[0], id[1], ..., id[n-1], given in strictly increasing order — the serial numbers.Print one line per storage block, in increasing order of starting number, as two space-separated integers start end — the smallest and largest serial number in that block. A block containing a single pallet has start == end.
id is strictly increasing (id[i] < id[i+1] for all valid i), so all values are distinct.Example 1
Input
6 1 2 3 6 7 9
Expected
1 3 6 7 9 9
Explanation
The numbers 1, 2, 3 are consecutive and form one block from 1 to 3. Then 6, 7 are consecutive and form a block from 6 to 7. Finally 9 has no neighbor differing by 1, so it forms its own block from 9 to 9.
Example 2
Input
1 5
Expected
5 5
Explanation
Only one pallet was scanned, so there is exactly one block consisting of that single serial number, giving start == end == 5.
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 →