Trains the technique from
LeetCode 2149Rearrange Array Elements by SignThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A bookkeeper is re-issuing a statement. The old statement is nums, one signed amount per line: a positive amount is a credit and a negative amount is a debit. No line is ever zero, the statement has an even number of lines, and it holds exactly as many credits as debits.
House style says the re-issued statement must read credit, debit, credit, debit, and so on, beginning with a credit. Within each kind, the bookkeeper must not shuffle anything: if one credit came before another credit on the old statement, it must still come before it on the new one, and the same holds for the debits.
Return the re-issued statement as a list of amounts in order. Exactly one arrangement satisfies house style.
Example 1
The old statement's credits are 3 then 4 and its debits are -1 then -2. The re-issued statement alternates from a credit and each kind is still in its old sequence.
Example 2
Credits appear as 9, 2, 5 and debits as -4, -7, -3. Reading the answer, the odd slots hold those debits in that same order and the even slots hold those credits in theirs.
Example 3
Two lines only. The credit takes the first slot and the debit the second.
Example 4
Both credits are 5 and both debits are -5, so any credit-first alternation matches house style, and the answer alternates from a credit.
Example 5
Here the old statement opens with all three debits. The re-issued one still begins with a credit, taking 3, 2, 1 in that order for the even slots and -1, -2, -3 for the odd slots.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def rearrange_array(nums: list[int]) -> list[int]:public int[] rearrangeArray(int[] nums)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.