Given many intervals as ranges and our position. We need to find minimum distance to travel to reach such a point which covers all the intervals at once.
Input : Intervals = [(0, 7), (2, 14), (4, 6)] Position = 3 Output : 1 We can reach position 4 by traveling distance 1, at which all intervals will be covered. So answer will be 1 Input : Intervals = [(1, 2), (2, 3), (3, 4)] Position = 2 Output : -1 It is not possible to cover all intervals at once at any point Input : Intervals = [(1, 2), (2, 3), (1, 4)] Position = 2 Output : 0 All Intervals are covered at current position only so no need travel and answer will be 0 All above examples are shown in below diagram.
We can solve this problem by concentrating only on endpoints. Since the requirement is to cover all intervals by reaching a point, all intervals must a share a point for answer to exist. Even the interval with leftmost end point must overlap with the interval right most start point.
First, we find right most start point and left most end point from all intervals. Then we can compare our position with these points to get the result which is explained below :
- If this right most start point is to the right of left most end point then it is not possible to cover all intervals simultaneously. (as in example 2)
- If our position is in mid between to right most start and left most end then there is no need to travel and all intervals will be covered by current position only (as in example 3)
- If our position is left to both points then we need to travel up to the rightmost start point and if our position is right to both points then we need to travel up to leftmost end point.
Refer above diagram to understand these cases. As in the first example, right most start is 4 and left most end is 6, so we need to reach 4 from current position 3 to cover all intervals.
Please see below code for better understanding.
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Minimum Cost Polygon Triangulation
- Optimum location of point to minimize total distance
- Paper Cut into Minimum Number of Squares
- Minimum lines to cover all points
- Minimum block jumps to reach destination
- Minimum revolutions to move center of a circle to a target
- Find points at a given distance on a line of given slope
- Minimum Perimeter of n blocks
- Find minimum radius such that atleast k point lie inside the circle
- Minimum area of a Polygon with three points given
- Minimum height of a triangle with given base and area
- Minimum number of points to be removed to get remaining points on one side of axis
- Program for distance between two points on earth
- Find maximum and minimum distance between magnets
- Ways to choose three points with distance between the most distant points <= L