FastPrepFastest Average Speed over a Rolling Kilometer

Fastest Average Speed over a Rolling Kilometer

Optiver logoOptiver● HardFULLTIMEOA
Learn

Problem statement

meters[i] is the distance traveled during the i-th consecutive five-second interval. Assume speed is constant within each interval.

Among every continuous segment of the run covering exactly 1000 meters, return the maximum average speed in meters per second. A segment boundary may split an interval proportionally. Return -1.0 when the total distance is less than 1000 meters.

Function

fastestKilometerSpeed(meters: int[]) → double

Examples

Example 1

meters = [400,400,400]return = 80.0

At 80 meters per second throughout, exactly 1000 meters takes 12.5 seconds.

Example 2

meters = [100,600,600,100]return = 120.0

The fastest kilometer starts and ends inside the two central intervals and takes 1000 / 120 seconds.

Example 3

meters = [200,300,400]return = -1.0

The complete run covers only 900 meters.

Constraints

  • 1 <= meters.length <= 100000.
  • 1 <= meters[i] <= 10000.
  • The answer is accepted within 10^-6 absolute or relative error.

More Optiver problems

See Optiver hiring insights
public double fastestKilometerSpeed(int[] meters) {
    // Write your solution here.
}
meters[400,400,400]
expected80.0
Checking account…