Here’s the Problem Statement.
Consider a line segment of length L. Its left end is the Origin and the right end is (L,0). You are supposed to answer Q Queries. In i^{th} query, a vertical line is drawn at (x_i, 0), cutting the line segment at (x_i, 0). Find the length of the largest segment after the query.
Example Input
L = 100
Q = 3
x_i = [30, 20, 60]
Example Output
[70, 70, 40]
Explanation
For the first query, we divide the line at (30, 0). Now the line segment is divided into 2 segments, (0, 30) and (30, 100). The length of the largest segment is 70.
For the second query, we divide the line at (20, 0). Now the line segment is divided into 3 segments, (0, 20), (20, 30) and (30, 100). The length of the largest segment is still 70.
For the third query, we divide the line at (60, 0). Now the line segment is divided into 4 segments, (0, 20), (20, 30), (30, 60) and (60, 100). The length of the largest segment is 40.
Any help is greatly appreciated.
PS: A variant of this problem is asked in Hackerrank Certification test long back. So, it is impossible to provide the link of the actual problem.