#K33752. Categorize Numbers into Ranges
Categorize Numbers into Ranges
Categorize Numbers into Ranges
You are given a series of test cases. In each test case, you are given a sequence of numbers preceded by an integer n indicating the count of numbers. Your task is to group consecutive numbers into ranges. For any sequence of consecutive numbers (i.e. numbers that follow one another with a difference of 1), output the range in the format start-end
. If a number does not have a consecutive neighbor, output it on its own.
For instance, given a test case with the numbers: 1, 2, 3, 7, 8, 10, the correct output is 1-3, 7-8, 10
because 1,2,3 form a continuous range, 7,8 form another range, and 10 stands alone.
Note: The input will be read from standard input (stdin) and the output should be printed to standard output (stdout).
inputFormat
The input begins with an integer T, the number of test cases. Each test case is provided on a separate line. Every test case starts with an integer n which represents the number of numbers to follow, followed by n space-separated integers.
Example:
1 6 1 2 3 7 8 10
outputFormat
For each test case, output a single line containing the categorized ranges. Consecutive numbers should be grouped as start-end
and individual numbers printed as is. The segments should be separated by a comma followed by a space.
Example:
1-3, 7-8, 10## sample
1
6 1 2 3 7 8 10
1-3, 7-8, 10
</p>