Problem
Rearrange nonzero integers to alternate positive and negative values while both are available. Append any leftover values at the end. Preserving order within each sign is a rule of this practice version.
Worked examples
Input: a = [2,-1,4,-3,6]
Output: [2,-1,4,-3,6]
The final positive value is appended after the alternating pairs.
Hints
Hint 1
Split values by sign before interleaving.
Solution approach
- Collect positive and negative values in their original order.
- Interleave one of each until either list ends, then append the remaining values.
- Ask how zero should be treated and whether relative order is required.
Complexity
O(n) time and O(n) space.
Report & practice notes
Restated practice version with original examples and explanation. The source is a candidate account, not an official question paper; assessment details can vary. Difficulty is our editorial estimate.
Read the candidate’s source report ↗