Reorder an array so positive and negative values appear in alternating order while preserving their original relative order within each sign group.
Given an integer array nums containing the same number of positive and negative integers, rearrange the elements so that:
Keep the relative order of the positive numbers the same as in the original array, and do the same for the negative numbers.
Return the rearranged array.
nums.nums contains an equal number of positive and negative integers.nums.nums.length is evennums[i] != 0Example 1
Input
nums = [3,1,-2,-5,2,-4]
Output
[3,-2,1,-5,2,-4]
Explanation
Positive numbers keep their order: [3,1,2]. Negative numbers keep their order: [-2,-5,-4]. Placing them alternately starting with a positive number gives [3,-2,1,-5,2,-4].
Example 2
Input
nums = [-1,1]
Output
[1,-1]
Explanation
The array must start with a positive number, so the only valid rearrangement is [1,-1].
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.