Minimum distance a person has to move in order to take a picture of every racer | C ++ implementation of the approach ; Function to return the minimum distance the person has to move int order to get the pictures ; To store the minimum ending point ; To store the maximum starting point ; Find the values of minSeg and maxSeg ; Impossible ; The person doesn 't need to move ; Get closer to the left point ; Get closer to the right point ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }