Requirements
- You receive a numeric array whose elements are already arranged in sequence.
- Traverse it from left to right, combining neighboring values whenever each next value is exactly 1 greater than the previous one.
- For any combined sequence containing two or more numbers, output it as
first to last.
- A sequence containing just one number must be output as that number alone.
- Write every resulting compressed range on a separate line.
Examples
Input:
[4,5,9,10,11,14,20]
Output:
4 to 5
9 to 11
14
20
The values 4 and 5 form one consecutive range, as do 9, 10, and 11; 14 and 20 have no adjacent successor in the input, so each is printed by itself.
Notes
- Do not rearrange the array before processing it. Sorting changes the intended behavior, since results must reflect the given ordering.
- Only groups with at least two elements qualify for range formatting (
length >= 2).