FunctionRacerProblemsRace

Alternate the Signs

EasyArraysPython, JavaScript, TypeScript, Java, C++

nums holds positive and negative numbers, never zero. Build a new list that alternates between a positive and a negative number, starting with a positive one. The positives keep the relative order they had in nums, and so do the negatives.

When one kind runs out, or was never there, the remaining numbers of the other kind follow in their original order. Return the new list; an empty nums gives an empty list.

Example 1

nums [3,-1,-4,2,5]
returns [3,-1,2,-4,5]

Positives in order: 3, 2, 5. Negatives in order: -1, -4. Alternate them, then the leftover 5 closes the list.

Example 2

nums [-2,-3,4]
returns [4,-2,-3]

The only positive leads, then both negatives follow in order.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems