Interleave Signs With Stable Order and Trailing Zeros
Arrays & HashingAuto-graded · 19 test casesLast updated
Build a new list that alternates positives and negatives (starting positive), keeps same-sign order, appends leftover elements, and places zeros last.
Interleave Signs With Stable Order and Trailing Zeros
You are given a list of integers values. Return a new list that contains the same elements, arranged by the following rules.
Rules
- Alternation. The result begins with a positive number (if any positive exists) and then alternates: positive, negative, positive, negative, and so on. This continues for as long as both a positive and a negative number are still available.
- Leftovers. Once one sign runs out, the remaining numbers of the other sign follow immediately, in their original order. At most one sign has leftovers.
- Zeros last. Every zero goes at the very end of the result, after everything else. Zeros are neither positive nor negative and do not take part in the alternation.
- Stability. Among positives, the relative order from the input is preserved. The same holds for negatives and for zeros.
The starting sign is always positive when at least one positive exists, even if negatives are more numerous.
The output must match exactly, so follow these rules precisely. The result must be a permutation of the input.
Function
Implement interleave_signs(values) that returns the rearranged list.
Constraints
1 <= len(values) <= 10^5-10^9 <= values[i] <= 10^9- Aim for
O(n)time.
Examples
Example 1
Input: interleave_signs([1, -2, 3, -4, 0, 5])
Output: [1, -2, 3, -4, 5, 0]
Example 2
Input: interleave_signs([-1, -2, 3, 0])
Output: [3, -1, -2, 0]
Example 3
Input: interleave_signs([0, 0, -3, 2, 4, -1, 5])
Output: [2, -3, 4, -1, 5, 0, 0]
Hints
Hint 1
Split the input into three lists by sign (positive, negative, zero), each keeping the input order.
Hint 2
Walk two pointers over the positive and negative lists, emitting one from each per step while both still have elements.
Hint 3
After the paired phase, append whatever remains of either sign list, and only then append all the zeros.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Separate the input into three lists in a single left-to-right pass each: positives, negatives, and zeros. Each list keeps the input order, which gives stability for free.
Then build the answer:
- While both the positive list and the negative list still have unused elements, append one positive and then one negative.
- Append the unused tail of the positive list and the unused tail of the negative list. Only one of these can be non-empty.
- Append all zeros.
Why it is correct
- Alternation: The paired loop runs exactly
min(P, N)times, wherePandNare the counts of positives and negatives. Each round adds a positive followed by a negative, so the prefix alternates and starts with a positive. - Leftovers: When the loop ends, one of the two lists is exhausted, so only the other contributes a tail. That tail keeps its input order.
- Zeros: They are appended last, in input order.
- Completeness: Every element belongs to exactly one list and every list is fully emitted, so the output is a permutation of the input.
Complexity
- Time:
O(n), from a few linear scans and one linear merge. - Space:
O(n)for the three lists and the result.
Trade-off note
An in-place approach (a three-way partition followed by swapping negatives into the odd positions) uses O(1) extra space, but it scrambles the order within each sign. Keeping the order while staying in place requires rotations (O(n^2)) or more elaborate block-based methods (O(n log n)). Since the required output is stable and n is at most 10^5, using extra memory is the practical choice.
Reference solution
from typing import List
def interleave_signs(values: List[int]) -> List[int]:
positives = [x for x in values if x > 0]
negatives = [x for x in values if x < 0]
zeros = [x for x in values if x == 0]
result = []
p = 0
n = 0
while p < len(positives) and n < len(negatives):
result.append(positives[p])
result.append(negatives[n])
p += 1
n += 1
result.extend(positives[p:])
result.extend(negatives[n:])
result.extend(zeros)
return result