Minimum distance a person has to move in order to take a picture of every racer
Consider a linear running track. Some racers are running on the track between a particular segment. A person is trying to take pictures of every racer. The person can take a picture of a racer if he stands anywhere between the racer’s running track segment i.e. between the start and end point of the racer. Given the starting and ending point of N racers and an integer D denoting the initial position of the person taking pictures. The task is to find the minimum distance the person has to move in order to take the picture of every racer from the final point. If it is impossible to take the pictures of every racer then print -1.
Examples:
Input: start[] = {0, 2, 4}, end[] = {7, 14, 6}, D = 3
Output: 1
The segments are:
0 . . . . . . 7
. . 2 . . . . . . . . . . . 14
. . . . 4 . 6
. . . d
Hence, the person has to move towards 4 i.e. 1 unit.
Input: start[] = {1, 2}, end[] = {2, 3}, D = 2
Output: 0
Approach: The above problem can be solved by observation, that the person has to be ahead of each racer before they start the race and finish it. So if he is in the range of the racer starting last and the racer ending first, he can take the picture, else not.
Find the maximum value of the starting point say left and the minimum value of the ending point say right among all the given racers. Now,
- left > right then it is impossible for the person to take pictures and print -1.
- If D is within the range [left, right] then the person doesn’t need to move and the answer will be 0.
- Else the person has to move min(abs(left – D), abs(right – D)).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minDistance( int start[], int end[], int n, int d)
{
int left = INT_MIN;
int right = INT_MAX;
for ( int i = 0; i < n; i++) {
left = max(left, start[i]);
right = min(right, end[i]);
}
if (left > right)
return -1;
if (d >= left && d <= right)
return 0;
if (d < left)
return (left - d);
if (d > right)
return (d - right);
}
int main()
{
int start[] = { 0, 2, 4 };
int end[] = { 7, 14, 6 };
int n = sizeof (start) / sizeof ( int );
int d = 3;
cout << minDistance(start, end, n, d);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int minDistance( int start[], int end[], int n,
int d)
{
int left = Integer.MIN_VALUE;
int right = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++) {
left = Math.max(left, start[i]);
right = Math.min(right, end[i]);
}
if (left > right)
return - 1 ;
if (d >= left && d <= right)
return 0 ;
if (d < left)
return (left - d);
if (d > right)
return (d - right);
return - 1 ;
}
public static void main(String[] args)
{
int start[] = { 0 , 2 , 4 };
int end[] = { 7 , 14 , 6 };
int n = start.length;
int d = 3 ;
System.out.println(minDistance(start, end, n, d));
}
}
|
Python3
import sys
def minDistance(start, end, n, d) :
left = - sys.maxsize
right = sys.maxsize
for i in range (n) :
left = max (left, start[i])
right = min (right, end[i])
if (left > right):
return - 1
if (d > = left and d < = right):
return 0
if (d < left) :
return (left - d)
if (d > right) :
return (d - right)
start = [ 0 , 2 , 4 ]
end = [ 7 , 14 , 6 ]
n = len (start)
d = 3
print (minDistance(start, end, n, d))
|
C#
using System;
class GFG{
static int minDistance( int [] start, int [] end,
int n, int d)
{
int left = Int32.MinValue;
int right = Int32.MaxValue;
for ( int i = 0; i < n; i++)
{
left = Math.Max(left, start[i]);
right = Math.Min(right, end[i]);
}
if (left > right)
return -1;
if (d >= left && d <= right)
return 0;
if (d < left)
return (left - d);
if (d > right)
return (d - right);
return -1;
}
public static void Main(String[] args)
{
int [] start = { 0, 2, 4 };
int [] end = { 7, 14, 6 };
int n = start.Length;
int d = 3;
Console.Write(minDistance(start, end, n, d));
}
}
|
Javascript
<script>
function minDistance(start, intend, n, d) {
let left = Number.MIN_SAFE_INTEGER;
let right = Number.MAX_SAFE_INTEGER;
for (let i = 0; i < n; i++) {
left = Math.max(left, start[i]);
right = Math.min(right, end[i]);
}
if (left > right) return -1;
if (d >= left && d <= right) return 0;
if (d < left) return left - d;
if (d > right) return d - right;
}
let start = [0, 2, 4];
let end = [7, 14, 6];
let n = start.length;
let d = 3;
document.write(minDistance(start, end, n, d));
</script>
|
Time complexity: O(N) where N is the number of racers
Auxiliary space: O(1)
Last Updated :
19 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...