Remove Element
Real-world version: Discard Fault SamplesThe statement here is our own: the same inputs, outputs and technique.
A soil probe writes one reading per minute into a fixed buffer samples. One reading value, fault, is a diagnostic code rather than a measurement, so every occurrence of it has to be squeezed out of the buffer.
Rewrite samples in place so that its first kept slots hold exactly the readings that differ from fault, keeping those readings in the order they were recorded. Slots from index kept onward may hold anything; nobody reads them. You may not allocate a second buffer that grows with the input, so the rewrite has to use O(1) extra space.
Because the grader can only inspect a return value, return the pair [kept, samples[:kept]]: the number of surviving readings followed by the surviving prefix itself. The prefix must be in recorded order, so the answer is unique.
Examples
Example 1
- Input
- samples = [4, 9, 4, 7, 4], fault = 4
- Output
- [2, [9, 7]]
Three diagnostic codes drop out; the two real readings slide left and keep their recorded order.
Example 2
- Input
- samples = [21, 8, 21, 21, 13, 8], fault = 21
- Output
- [3, [8, 13, 8]]
The buffer starts with a code and holds a run of two more in the middle, so the three survivors compact into the first three slots.
Example 3
- Input
- samples = [12], fault = 40
- Output
- [1, [12]]
No reading matches the code, so nothing moves and the whole buffer survives.
Constraints
- 0 <= samples.length <= 100
- 0 <= samples[i] <= 50
- 0 <= fault <= 100
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def discard_fault_samples(samples: list[int], fault: int) -> list:public int[] discardFaultSamples(int[] samples, int fault)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's