Compact the List
Learn this problemProblem statement
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.
Function
compactList(inputListSize: int, inputList: int[]) → String[]Examples
Example 1
inputListSize = 8inputList = [1, 2, 3, 6, 7, 8, 10, 15]return = ["1 to 3", "6 to 8", "10", "15"]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.
More Cisco problems
- Collect CoinsSeen Jun 2025
- FizzBuzz ProblemSeen May 2025
- Find Largest Sum Contiguous SubarraySeen May 2025
- Find Largest Sum of Continuous SequenceSeen May 2025
- Find Palindrome Sub-stringSeen May 2025
- Count Numbers with Digit SumSeen Mar 2025
- Maximum Chocolates from Jars (L.C. 198 :)Seen Mar 2025
- Find Elements Largest in Row Smallest in ColumnSeen Mar 2025