FastPrepPlace a Lamp to Cover the Most Objects

Place a Lamp to Cover the Most Objects

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA
Learn

Problem statement

objects contains unique coordinates in strictly increasing order. A lamp at integer coordinate x illuminates every object whose distance from x is at most radius.

Return an integer lamp coordinate that illuminates the most objects, breaking ties by the smallest coordinate.

Function

bestLampCoordinate(objects: int[], radius: int) → int

Examples

Example 1

objects = [-5,3,4,9]radius = 5return = -1

The window [-5,4] is covered by lamp coordinate -1 and contains three objects.

Example 2

objects = [7]radius = 2return = 5

The smallest lamp coordinate covering the object is five.

Constraints

  • 1 <= objects.length <= 100000
  • -1000000000 <= objects[i] <= 1000000000
  • 0 <= radius <= 1000000000

More ZipRecruiter problems

See ZipRecruiter hiring insights
public int bestLampCoordinate(int[] objects, int radius) {
    // Write your code here.
}
objects[-5,3,4,9]
radius5
expected-1
Checking account…