#C14145. Alternate Positive and Negative Reordering
Alternate Positive and Negative Reordering
Alternate Positive and Negative Reordering
Given a list of integers, reorder the list such that positive and negative numbers appear alternately, starting with a positive number. The relative order among positive numbers and among negative numbers should be preserved. If one group runs out before the other, append the remaining elements in their original order.
For example:
- Input: [1, 2, -1, -2, -3, 3, 4] → Output: [1, -1, 2, -2, 3, -3, 4]
- Input: [-1, -2, -3, 1, 2, 3, 4] → Output: [1, -1, 2, -2, 3, -3, 4]
- If the list contains only positives or only negatives, the output remains unchanged.
Note: When displaying formulas in the description, use LaTeX format. For example, the condition for a positive number is expressed as \(x > 0\) and for a negative number as \(x < 0\).
inputFormat
The first line contains an integer \(n\), representing the number of elements in the list. The second line contains \(n\) space-separated integers.
outputFormat
Output the reordered list as a sequence of space-separated integers in one line.
## sample7
1 2 -1 -2 -3 3 4
1 -1 2 -2 3 -3 4