Standard Bar Arrays · Medium O(n) · O(n) for the output (O(1) if written into the given array)
A battery test rig records charge steps as positive battery values and discharge steps as negative ones, with equal counts of each. To visualize the cycling profile, the log must be rewritten so charge and draw alternate, the sequence starts on a charge step, and steps of the same polarity keep their original relative order.
Input: An array nums of n integers with even length n, containing exactly n/2 positive and n/2 negative values.
Output: A rearranged array where values alternate signs starting with a positive one, preserving the original order within each polarity.
2 <= n <= 2 * 10^5, n is even-10^4 <= nums[i] <= 10^4, and nums[i] != 0Exactly n/2 values are positive and n/2 are negativeInput: {"nums":[3,1,-2,-5,2,-4]}
Output: [3,-2,1,-5,2,-4]
Positives keep the order 3,1,2 and negatives keep -2,-5,-4, interleaved at even and odd slots.
Input: {"nums":[-1,1]}
Output: [1,-1]
The single pair is placed positive-first even though the input leads with a negative value.