You are given an even-length integer array nums with as many positive as negative integers. Rearrange it so that signs alternate starting with a positive number, while the positives keep their original relative order and the negatives keep theirs. Return the rearranged array.
You are given an even-length integer array nums with as many positive as negative integers. Rearrange it so that signs alternate starting with a positive number, while the positives keep their original relative order and the negatives keep theirs. Return the rearranged array.
Example 1:
Input: {"nums":[3,1,-2,-5,2,-4]}
Output: [3,-2,1,-5,2,-4]
Constraints:
2≤nums.length≤2⋅105
nums.length is even
1≤∣nums[i]∣≤105
nums has equally many positive and negative integers.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[3,1,-2,-5,2,-4]}
Make an output of 6 slots. Even slots (0, 2, 4, ...) will hold positives and odd slots (1, 3, 5, ...) negatives.