Minimum distance to travel to cover all intervals
Last Updated :
29 Oct, 2022
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.
Examples:
Input : Intervals = [(0, 7), (2, 14), (4, 6)]
Position = 3
Output : 1
We can reach position 4 by travelling
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 the below code for a better understanding.
C++
#include <bits/stdc++.h>
using namespace std;
struct Interval
{
int start, end;
Interval( int start, int end) : start(start),
end(end)
{}
};
int minDistanceToCoverIntervals(Interval intervals[],
int N, int x)
{
int rightMostStart = INT_MIN;
int leftMostEnd = INT_MAX;
for ( int i = 0; i < N; i++)
{
if (rightMostStart < intervals[i].start)
rightMostStart = intervals[i].start;
if (leftMostEnd > intervals[i].end)
leftMostEnd = intervals[i].end;
}
int res;
if (rightMostStart > leftMostEnd)
res = -1;
else if (rightMostStart <= x && x <= leftMostEnd)
res = 0;
else
res = (x < rightMostStart) ? (rightMostStart - x) :
(x - leftMostEnd);
return res;
}
int main()
{
int x = 3;
Interval intervals[] = {{0, 7}, {2, 14}, {4, 6}};
int N = sizeof (intervals) / sizeof (intervals[0]);
int res = minDistanceToCoverIntervals(intervals, N, x);
if (res == -1)
cout << "Not Possible to cover all intervals\n" ;
else
cout << res << endl;
}
|
Java
import java.util.*;
class GFG{
static class Interval
{
int start, end;
Interval( int start, int end)
{
this .start = start;
this .end = end;
}
};
static int minDistanceToCoverIntervals(Interval intervals[],
int N, int x)
{
int rightMostStart = Integer.MIN_VALUE;
int leftMostEnd = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++)
{
if (rightMostStart < intervals[i].start)
rightMostStart = intervals[i].start;
if (leftMostEnd > intervals[i].end)
leftMostEnd = intervals[i].end;
}
int res;
if (rightMostStart > leftMostEnd)
res = - 1 ;
else if (rightMostStart <= x &&
x <= leftMostEnd)
res = 0 ;
else
res = (x < rightMostStart) ?
(rightMostStart - x) :
(x - leftMostEnd);
return res;
}
public static void main(String[] args)
{
int x = 3 ;
Interval []intervals = { new Interval( 0 , 7 ),
new Interval( 2 , 14 ),
new Interval( 4 , 6 ) };
int N = intervals.length;
int res = minDistanceToCoverIntervals(
intervals, N, x);
if (res == - 1 )
System.out.print( "Not Possible to " +
"cover all intervals\n" );
else
System.out.print(res + "\n" );
}
}
|
Python3
def minDistanceToCoverIntervals(Intervals, N, x):
rightMostStart = Intervals[ 0 ][ 0 ]
leftMostStart = Intervals[ 0 ][ 1 ]
for curr in Intervals:
if rightMostStart < curr[ 0 ]:
rightMostStart = curr[ 0 ]
if leftMostStart > curr[ 1 ]:
leftMostStart = curr[ 1 ]
if rightMostStart > leftMostStart:
res = - 1
else if rightMostStart < = x and x < = leftMostStart:
res = 0
else :
res = rightMostStart - x if x < rightMostStart else x - leftMostStart
return res
Intervals = [[ 0 , 7 ], [ 2 , 14 ], [ 4 , 6 ]]
N = len (Intervals)
x = 3
res = minDistanceToCoverIntervals(Intervals, N, x)
if res = = - 1 :
print ( "Not Possible to cover all intervals" )
else :
print (res)
|
C#
using System;
class GFG{
public class Interval
{
public int start, end;
public Interval( int start, int end)
{
this .start = start;
this .end = end;
}
};
static int minDistanceToCoverIntervals(
Interval []intervals, int N, int x)
{
int rightMostStart = int .MinValue;
int leftMostEnd = int .MaxValue;
for ( int i = 0; i < N; i++)
{
if (rightMostStart < intervals[i].start)
rightMostStart = intervals[i].start;
if (leftMostEnd > intervals[i].end)
leftMostEnd = intervals[i].end;
}
int res;
if (rightMostStart > leftMostEnd)
res = -1;
else if (rightMostStart <= x &&
x <= leftMostEnd)
res = 0;
else
res = (x < rightMostStart) ?
(rightMostStart - x) :
(x - leftMostEnd);
return res;
}
public static void Main(String[] args)
{
int x = 3;
Interval []intervals = { new Interval(0, 7),
new Interval(2, 14),
new Interval(4, 6) };
int N = intervals.Length;
int res = minDistanceToCoverIntervals(
intervals, N, x);
if (res == -1)
Console.Write( "Not Possible to " +
"cover all intervals\n" );
else
Console.Write(res + "\n" );
}
}
|
Javascript
<script>
function minDistanceToCoverIntervals(Intervals, N, x){
let rightMostStart = Intervals[0][0]
let leftMostStart = Intervals[0][1]
for (let curr of Intervals){
if (rightMostStart < curr[0])
rightMostStart = curr[0]
if (leftMostStart > curr[1])
leftMostStart = curr[1]
}
let res;
if (rightMostStart > leftMostStart)
res = -1
else if (rightMostStart <= x && x <= leftMostStart)
res = 0
else
res = (x < rightMostStart)?rightMostStart-x : x-leftMostStart
return res
}
let Intervals = [[0, 7], [2, 14], [4, 6]]
let N = Intervals.length
let x = 3
let res = minDistanceToCoverIntervals(Intervals, N, x)
if (res == -1)
document.write( "Not Possible to cover all intervals" , "<br>" )
else
document.write(res)
</script>
|
Output:
1
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...