Loading
Loading
A ticketing system stores queue positions in an array. Cancelled tickets are recorded as the value 0.
Remove every cancelled ticket by shifting the remaining values toward the front, keeping their original relative order. Return the number of tickets that remain. Values beyond that count do not matter.
tickets = [3, 0, 7, 0, 0, 12][3, 0, 7, 0, 0, 12]write = 0, read 3 → keep[3, 0, 7, 0, 0, 12]write = 1, read 0 → skip[3, 7, 7, 0, 0, 12]write = 1, read 7 → keep[3, 7, 12, ...]write = 2, read 12 → keep3Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n) time · O(1) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
The surviving tickets are 3, 7 and 12.
Example 2
Every ticket was cancelled.