def install_lightbulbs(M, R, street_lights): street_lights.sort() n = len(street_lights) i = 0 num_bulbs = 0 rightmost_illuminated = -1 while i < n: if street_lights[i] - R > rightmost_illuminated: return -1 # there is a gap that cannot be illuminated leftmost_possible = street_lights[i] while i < n and street_lights[i] - R <= leftmost_possible: i += 1 rightmost_illuminated = min(M, leftmost_possible + 2*R) num_bulbs += 1 return num_bulbs