Minimum and maximum count of elements at D distance from arr [ i ] in either direction | ''Function to find the minimum and maximum number of points included in a range of distance D ; '' Stores the minimum and maximum  number of points that lies  over the distance of D ; '' Iterate the array ; '' Count of elements included  to left of point at index i ; '' Update the minimum number  of points ; '' Update the maximum number  of points ; '' Count of elements included  to right of point at index i ; '' Update the minimum number  of points ; '' Update the maximum number  of points ; '' Return the array ; ''Function to perform the Binary Search to the left of arr[i] over the given range ; '' Base Case ; '' Binary Search for index to left ; '' update index ; '' Return the number of elements  by subtracting indices ; ''Function to perform the Binary Search to the right of arr[i] over the given range ; '' Base Case ; '' Binary Search for index to right ; '' Update the index ; '' Return the number of elements  by subtracting indices ; ''Driver Code ; ''Function Call
def minMaxRange ( arr , D , N ) : NEW_LINE INDENT Max = 1 NEW_LINE Min = N NEW_LINE for i in range ( N ) : NEW_LINE INDENT dist = leftSearch ( arr , arr [ i ] - D , i ) NEW_LINE Min = min ( Min , dist ) NEW_LINE Max = max ( Max , dist ) NEW_LINE dist = rightSearch ( arr , arr [ i ] + D , i ) NEW_LINE Min = min ( Min , dist ) NEW_LINE Max = max ( Max , dist ) NEW_LINE DEDENT return [ Min , Max ] NEW_LINE DEDENT def leftSearch ( arr , val , i ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT left = 0 NEW_LINE right = i - 1 NEW_LINE ind = - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE if ( arr [ mid ] < val ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE ind = mid NEW_LINE DEDENT DEDENT return i - ind + 1 if ind != - 1 else 1 NEW_LINE DEDENT def rightSearch ( arr , val , i ) : NEW_LINE INDENT if ( i == len ( arr ) - 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT left = i + 1 NEW_LINE right = len ( arr ) - 1 NEW_LINE ind = - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE if ( arr [ mid ] > val ) : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE ind = mid NEW_LINE DEDENT DEDENT return ind - i + 1 if ind != - 1 else 1 NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 9 , 14 ] NEW_LINE N = len ( arr ) NEW_LINE D = 4 NEW_LINE minMax = minMaxRange ( arr , D , N ) NEW_LINE print ( f " { minMax [ 0 ] } ▁ { minMax [ 1 ] } " ) NEW_LINE