FastPrepFastPrep
Problem Brief

Compact the List

INTERNOA
See Cisco online assessment and hiring insights

Given a sorted list of integers with no duplicates, write an algorithm to compact the list based on a continuous range of numbers. If there are no such ranges available, print the list of strings where each element is a string notation of the number.

Input

The first line of input consists of an integer inputListSize, representing the number of elements in the list (N). The next line consists of N space-separated integers representing the elements of the list.

Output

Print a line-separated list of strings which represents the compacted form of the given list based on a continuous range of numbers.

Note

List is sorted in ascending order.

1Example 1

Input
inputListSize = 8, inputList = [1, 2, 3, 6, 7, 8, 10, 15]
Output
["1 to 3", "6 to 8", "10", "15"]
Explanation

In the given list 1,2,3 form a continuous range and hence are compacted to "1 to 3" and the same for 6,7,8 which are compacted to "6 to 8". But 10 and 15 cannot be compacted so are printed as they are.

public String[] compactList(int size, int[] inputList) {
  // write your code here
}
Input

inputListSize

8

inputList

[1, 2, 3, 6, 7, 8, 10, 15]

Output

["1 to 3", "6 to 8", "10", "15"]

Sign in to submit your solution.