Minimum distance a person has to move in order to take a picture of every racer | 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
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 ) ) ;