Alternate non-negative and negative

Write codeMedium25 min

Your task

Given an integer array a, rearrange it so that elements alternate starting with a non-negative element: non-negative, negative, non-negative, negative, ... The relative order among the non-negatives and among the negatives must be preserved. If one group runs out, append the remaining elements of the other group in order. Treat 0 as non-negative.

Examples

rearrange_alternate(a: list[int]) -> list[int]
Example 1
Inputrearrange_alternate([1, 2, 3, -4, -1, 4])
Output[1, -4, 2, -1, 3, 4]