Dsa
Binary Search
Easy
Planting Trees on a Line
You are planting trees along a straight line. Given the positions where existing trees are planted, find out the maximum distance at which you can plant a new tree, such that it is no closer than D distance from any existing trees.
Input:
- The first line contains the integer T (1 ≤ T ≤ 10^5), the number of existing trees.
- The second line contains T space-separated integers representing the positions of the trees.
- The third line contains an integer D (1 ≤ D ≤ 10^9), the minimum distance required from existing trees.
Output:
- A single integer representing the maximum position available for planting a new tree.
Example:
Input:
5
1 5 10 14 20
2
Output:
22
Key concepts
binary_searchgeometryarrays
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now