#C14498. Group Elements
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:
- The first line contains a boolean flag (either
True
orFalse
) that indicates the grouping criteria. - The second line contains an integer n, representing the number of integers in the list.
- 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.
## sampleTrue
8
0 -3 7 2 -1 -3 5 0
0 0 -3 -1 -3 7 2 5