FastPrepBest Sprinkler Coordinate for Maximum Coverage

Best Sprinkler Coordinate for Maximum Coverage

Hudson River Trading logoHudson River Trading● MediumNEW GRADOA
Learn

Problem statement

You are given a strictly increasing integer array flowers containing flower coordinates on a number line and a nonnegative integer radius.

Place one sprinkler at any integer coordinate c. It waters every flower whose coordinate lies in the inclusive interval [c - radius, c + radius].

Return a coordinate that waters the maximum possible number of flowers. If several coordinates water that maximum number, return the smallest such coordinate.

Function

bestSprinklerCoordinate(flowers: int[], radius: int) → int

Examples

Example 1

flowers = [-6,-1,0,8]radius = 4return = -4

A sprinkler at -4 waters the three flowers at -6, -1, and 0. No position waters all four, and -4 is the smallest position that waters three.

Example 2

flowers = [-3,2,3,4,9]radius = 2return = 2

A sprinkler at 2 covers [0,4] and waters the three flowers at 2, 3, and 4.

Example 3

flowers = [7]radius = 3return = 4

Every sprinkler coordinate from 4 through 10 waters the only flower. The smallest valid coordinate is 4.

Constraints

  • 1 <= flowers.length <= 2 * 10^5.
  • -10^9 <= flowers[i] <= 10^9.
  • flowers is strictly increasing.
  • 0 <= radius <= 10^9.

More Hudson River Trading problems

See Hudson River Trading hiring insights
public int bestSprinklerCoordinate(int[] flowers, int radius) {
    // Write your code here.
}
flowers[-6,-1,0,8]
radius4
expected-4
Checking account…