Open In App

Minimum distance a person has to move in order to take a picture of every racer

Improve
Improve
Like Article
Like
Save
Share
Report

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:
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:

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the minimum
// distance the person has to move
// int order to get the pictures
int minDistance(int start[], int end[], int n, int d)
{
 
    // To store the minimum ending point
    int left = INT_MIN;
 
    // To store the maximum starting point
    int right = INT_MAX;
 
    // Find the values of minSeg and maxSeg
    for (int i = 0; i < n; i++) {
        left = max(left, start[i]);
        right = min(right, end[i]);
    }
 
    // Impossible
    if (left > right)
        return -1;
 
    // The person doesn't need to move
    if (d >= left && d <= right)
        return 0;
 
    // Get closer to the left point
    if (d < left)
        return (left - d);
 
    // Get closer to the right point
    if (d > right)
        return (d - right);
}
 
// Driver code
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




// Java implementation for the above approach
import java.io.*;
 
class GFG
{
   
    // Function to return the minimum
    // distance the person has to move
    // int order to get the pictures
    static int minDistance(int start[], int end[], int n,
                           int d)
    {
 
        // To store the minimum ending point
        int left = Integer.MIN_VALUE;
 
        // To store the maximum starting point
        int right = Integer.MAX_VALUE;
 
        // Find the values of minSeg and maxSeg
        for (int i = 0; i < n; i++) {
            left = Math.max(left, start[i]);
            right = Math.min(right, end[i]);
        }
 
        // Impossible
        if (left > right)
            return -1;
 
        // The person doesn't need to move
        if (d >= left && d <= right)
            return 0;
 
        // Get closer to the left point
        if (d < left)
            return (left - d);
 
        // Get closer to the right point
        if (d > right)
            return (d - right);
 
        return -1;
    }
 
  // Driver code
    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));
    }
}
 
// This code is contributed by Potta Lokesh


Python3




# Python3 program for the above approach
import sys
 
# Function to return the minimum
# distance the person has to move
# order to get the pictures
def minDistance(start, end, n, d) :
 
    # To store the minimum ending point
    left = -sys.maxsize
 
    # To store the maximum starting point
    right = sys.maxsize
 
    # Find the values of minSeg and maxSeg
    for i in range(n) :
        left = max(left, start[i])
        right = min(right, end[i])
     
 
    # Impossible
    if (left > right):
        return -1
 
    # The person doesn't need to move
    if (d >= left and d <= right):
        return 0
 
    # Get closer to the left point
    if (d < left) :
        return (left - d)
 
    # Get closer to the right point
    if (d > right) :
        return (d - right)
 
# Driver code
start = [ 0, 2, 4 ]
end = [ 7, 14, 6 ]
n = len(start)
d = 3
 
print(minDistance(start, end, n, d))
 
# This code is contributed by target_2.


C#




// C# program for above approach
using System;
 
class GFG{
 
// Function to return the minimum
// distance the person has to move
// int order to get the pictures
static int minDistance(int[] start, int[] end,
                       int n, int d)
{
     
    // To store the minimum ending point
    int left = Int32.MinValue;
 
    // To store the maximum starting point
    int right = Int32.MaxValue;
 
    // Find the values of minSeg and maxSeg
    for(int i = 0; i < n; i++)
    {
        left = Math.Max(left, start[i]);
        right = Math.Min(right, end[i]);
    }
 
    // Impossible
    if (left > right)
        return -1;
 
    // The person doesn't need to move
    if (d >= left && d <= right)
        return 0;
 
    // Get closer to the left point
    if (d < left)
        return (left - d);
 
    // Get closer to the right point
    if (d > right)
        return (d - right);
 
    return -1;
}
 
// Driver Code
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));
}
}
 
// This code is contributed by sanjoy_62


Javascript




<script>
 
// JavaScript implementation of the approach
 
// Function to return the minimum
// distance the person has to move
// int order to get the pictures
function minDistance(start, intend, n, d) {
  // To store the minimum ending point
  let left = Number.MIN_SAFE_INTEGER;
 
  // To store the maximum starting point
  let right = Number.MAX_SAFE_INTEGER;
 
  // Find the values of minSeg and maxSeg
  for (let i = 0; i < n; i++) {
    left = Math.max(left, start[i]);
    right = Math.min(right, end[i]);
  }
 
  // Impossible
  if (left > right) return -1;
 
  // The person doesn't need to move
  if (d >= left && d <= right) return 0;
 
  // Get closer to the left point
  if (d < left) return left - d;
 
  // Get closer to the right point
  if (d > right) return d - right;
}
 
// Driver code
 
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>


Output

1

Time complexity: O(N) where N is the number of racers
Auxiliary space: O(1)



Last Updated : 19 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads