Description
Rearrange array of equal positive and negative numbers so they alternate, starting with positive, maintaining relative order within signs.
Examples
Input:
nums = [3,1,-2,-5,2,-4]Output:
[3,-2,1,-5,2,-4]Explanation:
Alternating +/-.
Input:
nums = [-1,1]Output:
[1,-1]Explanation:
Works with negative numbers.
Input:
nums = [7,-3,4,-8,9,-1,2,-6]Output:
[7,-3,4,-8,9,-1,2,-6]Explanation:
The array already alternates between positive and negative starting with positive, so no rearrangement is needed.
Constraints
- •
2 ≤ nums.length ≤ 2 × 10⁵