All problems
1002EasyArrayHash Table

The Shortest Stretch Holding the Commonest Reading

Tracked in this browser only
Write code

Trains the technique from

LeetCode 697Degree of an Array

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 log holds the readings readings. Its busiest count is the number of times its most repeated reading appears.

Return the length of the shortest stretch of neighbouring entries whose own busiest count matches the whole log's.

Examples

Example 1

Input
readings = [4, 7, 7, 9, 4]
Output
2

The busiest count is two, reached by both 4 and 7. The two 4s sit at the far ends and need the whole log, while the two 7s sit side by side, so the shorter stretch wins.

Example 2

Input
readings = [3, 3, 3, 1, 5]
Output
3

Only 3 reaches the busiest count of three, and its appearances run from the start to the third entry.

Example 3

Input
readings = [8]
Output
1

One entry, so the busiest count is one and a single entry already matches it.

Constraints

  • 1 <= readings.length <= 50000
  • 0 <= readings[i] <= 49999

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 find_shortest_sub_array(readings: list[int]) -> int:
Java
public int findShortestSubArray(int[] readings)
September 7
Apply