A power-storage room lines up n capacitor banks in a single row, each holding some amount of stored charge (a non-negative integer; a bank holding 0 is empty). A technician sweeps the row exactly once, strictly left to right. At each position, if the bank there and the very next bank currently hold equal, non-zero charge, the technician fuses them: the left bank's charge doubles and the right bank drains fully to 0. The sweep always reads the row's live state as it proceeds, so a bank that was just drained (or just doubled) is compared using its new value, not the value it started with. Once the single sweep finishes, every drained (0-charge) bank is pushed to the right end of the row, while the remaining banks keep their original left-to-right relative order. Report the final charge held in each position.
Line 1: a single integer n — the number of capacitor banks.
Line 2: n space-separated integers charge_1 ... charge_n — the charge currently stored in each bank, from left to right.
Print n space-separated integers: the charge stored in each bank, from left to right, after the fusing sweep and the compaction of drained banks to the right end.
1 <= n <= 20000 <= charge_i <= 1000Example 1
Input
5 2 2 2 4 4
Expected
4 2 8 0 0
Explanation
Sweep: position 0-1 both hold 2, non-zero, so bank 0 becomes 4 and bank 1 drains to 0. Position 1-2: bank 1 is now 0 and bank 2 holds 2, not equal, no fuse. Position 2-3: 2 vs 4, not equal. Position 3-4: both hold 4, non-zero, so bank 3 becomes 8 and bank 4 drains to 0. Row after the sweep: [4,0,2,8,0]. Pushing the two drained (0) banks to the end while keeping [4,2,8] in order gives the final row [4,2,8,0,0].
Example 2
Input
4 0 0 3 3
Expected
6 0 0 0
Explanation
Position 0-1: bank 0 already holds 0, so even though bank 1 also holds 0, the fuse rule (which requires non-zero charge) does not apply. Position 1-2: 0 vs 3, not equal. Position 2-3: both hold 3, non-zero, so bank 2 becomes 6 and bank 3 drains to 0. Row after the sweep: [0,0,6,0]. Compacting pushes the three drained banks to the end while the single non-zero bank keeps its relative position, giving the final row [6,0,0,0].
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →