Trains the technique from
LeetCode 3634Minimum Removals to Balance ArrayThis 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 test bench has produced a batch of readings, listed as readings, and a tolerance factor k.
A batch counts as settled when it still holds at least one reading and its largest reading is no more than k times its smallest reading.
You may throw away any readings you choose. What is left over is judged only on the readings still in it, whatever positions they came from.
Return the smallest number of readings you have to throw away to leave a settled batch.
Example 1
Throwing away the reading 1 leaves 10, 9 and 8. The largest of those is 10 and the smallest is 8, and 10 is no more than twice 8, so the batch is settled after one discard.
Example 2
The batch already holds 4 as its largest and 2 as its smallest, and 4 is exactly twice 2, which the factor allows, so nothing has to go.
Example 3
With a factor of 1 a settled batch has to have every reading equal, and no reading is repeated here, so only one reading can stay and the other two are thrown away.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def fewest_discards(readings: list[int], k: int) -> int:public int fewestDiscards(int[] readings, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.