#C14498. Group Elements

    ID: 44153 Type: Default 1000ms 256MiB

Group Elements

Group Elements

You are given a list of integers and a boolean flag. Your task is to group the elements of the list according to a specified criterion. If the flag is True, you need to group the numbers by their sign: first all zero elements, then the negatives (in their original order), followed by the positives (in their original order).

If the flag is False, you need to group the numbers by their parity: first all even numbers (again preserving their order), and then all odd numbers (also preserving their order).

For example:

  • When grouping by type (True), the list [0, -3, 7, 2, -1, -3, 5, 0] should result in [0, 0, -3, -1, -3, 7, 2, 5].
  • When grouping by parity (False), the same list should result in [0, 2, 0, -3, 7, -1, -3, 5].

inputFormat

The input is given via standard input and has the following format:

  1. The first line contains a boolean flag (either True or False) that indicates the grouping criteria.
  2. The second line contains an integer n, representing the number of integers in the list.
  3. The third line contains n space-separated integers.

outputFormat

Output the resulting grouped list of integers on a single line, with each element separated by a space. If the list is empty, output an empty line.

## sample
True
8
0 -3 7 2 -1 -3 5 0
0 0 -3 -1 -3 7 2 5