Javascript program for the above approach
function upper_bound ( arr , key ) { let l = - 1 , r = arr . length ; while ( l + 1 < r ) { let m = ( l + r ) >>> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; }