Problem 302
Alternate Positive and Negative
Easy · 10 pts · Array, Two Pointers, Simulation
Problem
An array contains an equal number of positive and negative integers (zero never appears). Rearrange it so that positive and negative numbers alternate, starting with a positive number. Inside each group the original order must be kept: the positives appear in the same order as in the input, and so do the negatives. Print the rearranged array.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1]. N is even, and exactly N / 2 of the numbers are positive.
Output
Print the rearranged array, separated by spaces.
Constraints
2 <= N <= 100000; N is even; 1 <= |A[i]| <= 1000000000
Example input
6 3 1 -2 -5 2 -4
Example output
3 -2 1 -5 2 -4
Explanation
The positives in order are 3, 1, 2 and the negatives in order are -2, -5, -4. Taking one from each group in turn gives 3 -2 1 -5 2 -4.