If arr [ i ] is not smallest element and just smaller element is not k distance away
if ( s > 0 and a [ s - 1 ] - a [ i ] <= k ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT res += 1 ; NEW_LINE return res ; NEW_LINE def lower_bound ( a , low , high , element ) : NEW_LINE while ( low < high ) : NEW_LINE middle = int ( low + int ( high - low ) / 2 ) ; NEW_LINE if ( element > a [ middle ] ) : NEW_LINE INDENT low = middle + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = middle ; NEW_LINE DEDENT return low ; NEW_LINE def upper_bound ( a , low , high , element ) : NEW_LINE while ( low < high ) : NEW_LINE middle = int ( low + ( high - low ) / 2 ) ; NEW_LINE if ( a [ middle ] > element ) : NEW_LINE INDENT high = middle ; NEW_LINE DEDENT else : NEW_LINE INDENT low = middle + 1 ; NEW_LINE DEDENT return low ; NEW_LINE