← 返回 cisco 的题目列表Compress Consecutive Ranges
类型:qbank
Given an array, print consecutive runs as `a to b` and singleton values on their own line.
Requirements
Input is an ordered array of numbers.
Scan the input in order and group consecutive runs where adjacent values increase by 1.
If a run has length at least 2, print start to end.
Otherwise print the single value.
Print each compressed segment on its own line.
Examples
Input:
[1,2,3,6,7,8,10,15]
Output:
1 to 3
6 to 8
10
15
Notes
Preserve the input order. One candidate sorted the input and failed hidden cases because the platform expected the original order to be respected.
The shared statement uses length >= 2 as the compression condition.