Chocolate Packets — Move Empty Packets to End
E · easyP · Verified PYQarrays
Problem
A factory produces N chocolate packets. Empty (defective) packets are represented as 0. Push all empty packets (0s) to the end of the array while maintaining the relative order of non-zero (filled) packets. Input: First line = N (number of packets). Second line = N space-separated integers.
Constraints
1 <= N <= 10\*\*5
Example
Input
8 4 5 0 1 9 0 5 0
Output
4 5 1 9 5 0 0 0
Use a write pointer j=0. Copy non-zero elements to arr[j++]. Fill remaining indices with 0.