#C14846. Group and Sort Contiguous Integers

    ID: 44540 Type: Default 1000ms 256MiB

Group and Sort Contiguous Integers

Group and Sort Contiguous Integers

Given a list of integers, your task is to group them into sublists of contiguous integers, where two numbers are considered contiguous if their difference is exactly 1. After grouping, sort each sublist and then sort the overall list of groups based on the first element of each sublist. The sorting order (ascending or descending) is determined by a boolean flag.

For example:

  • If the input list is [1, 2, 4, 5, 6, 8, 9, 10, 12] and the flag is False, the output should be [[1, 2], [4, 5, 6], [8, 9, 10], [12]].
  • If the input list is [5, 1, 10, 2, 4, 9, 8, 6, 12] and the flag is True, the output should be [[12], [10, 9, 8], [6, 5, 4], [2, 1]].

Note that duplicate numbers are treated as individual entries. For instance, an input of [3, 3, 3, 3, 3] should output [[3], [3], [3], [3], [3]], because there is no difference of 1 between the adjacent items.

inputFormat

Input is provided via standard input (stdin). The first line contains an integer n representing the number of integers. The second line contains n space-separated integers. The third line contains a boolean flag (True or False) that determines the sorting order (reverse if True).

outputFormat

Output the computed list of groups as a Python-style list of lists to standard output (stdout). For example, the output should look like: [[1, 2], [4, 5, 6], [8, 9, 10], [12]].## sample

9
1 2 4 5 6 8 9 10 12
False
[[1, 2], [4, 5, 6], [8, 9, 10], [12]]

</p>