Nearest smaller character to a character K from a Sorted Array | Function to return the nearest smaller letacter ; Stores the nearest smaller letacter ; Iterate till starts cross end ; Find the mid element ; Check if K is found ; Check if current letacter is less than K ; Increment the start ; Otherwise ; Increment end ; Return the letacter ; Driver Code
function bs ( ar , n , ele ) { let start = 0 ; let end = n - 1 ; let ch = ' ' ; while ( start <= end ) { let mid = start + Math . floor ( ( end - start ) / 2 ) ; if ( ar [ mid ] == ele ) end = mid - 1 ; else if ( ar [ mid ] < ele ) { ch = ar [ mid ] ; start = mid + 1 ; } else end = mid - 1 ; } return ch ; } let ar = [ ' ' , ' ' , ' ' , ' ' ] ; let n = ar . length ; let K = ' ' ; let ch = bs ( ar , n , K ) ; if ( ch == ' ' ) document . write ( " " ) ; else document . write ( ch ) ;