All problems
0132EasyArrayTwo Pointers

Discard Fault Samples

Tracked in this browser only
Write code

Trains the technique from

LeetCode 27Remove Element

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def discard_fault_samples(samples: list[int], fault: int) -> list:
Java
public int[] discardFaultSamples(int[] samples, int fault)
September 7
Apply