Trains the technique from
LeetCode 347Top K Frequent ElementsThis 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 field probe emits one signed calibration offset each time it is polled. The array readings holds every offset captured during a single shift, and k is how many of them a technician wants to look at.
Report the k distinct offsets that were emitted the greatest number of times. Those k values may be handed back in whatever order is convenient.
The shift log is filtered before it reaches you so the choice is never ambiguous: any offset left out of the answer was emitted strictly fewer times than any offset placed in it.
Example 1
Offset 4 lands three times and offset -2 twice, both ahead of 7 with its single appearance.
Example 2
Offset 1 was emitted four times, more than 8 at three, 3 at two and -9 at one.
Example 3
Nothing repeats and k covers the whole distinct set, so all four offsets are reported.
The values you return may be in any order.
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 frequent_offsets(readings: list[int], k: int) -> list[int]:public int[] frequentOffsets(int[] readings, int k)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.