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