Javascript implementation of above approach
function upperBound ( a , low , high , element ) { while ( low < high ) { let middle = low + Math . floor ( ( high - low ) / 2 ) ; if ( a [ middle ] > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; }