A strip of n memory cells holds integer charges; a cell holding 0 is empty. A compaction pass slides every non-empty cell toward the front, preserving their left-to-right order, and pushes all empty (0) cells to the end. Report how many cell positions hold a different value after the compaction than before it.
Line 1: an integer n.
Line 2: n space-separated integers, the cell charges.
A single integer: the number of positions whose value changed.
Example 1
Input
6 0 1 0 3 12 0
Expected
5
Explanation
Non-zeroes 1 3 12 slide to the front, giving 1 3 12 0 0 0. Comparing to 0 1 0 3 12 0, positions 0,1,2,3,4 all differ while position 5 stays 0, so 5 positions changed.
Example 2
Input
4 1 2 3 4
Expected
0
Explanation
There are no zeroes, so nothing moves and no position changes value: 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 →