JS program for the above approach
function upper_bound ( freq , f ) { let low = 0 , high = freq . length - 1 ; while ( low < high ) { let mid = Math . floor ( ( low + high ) / 2 ) ; if ( freq [ mid ] > f ) high = mid ; else low = mid + 1 ; } return ( freq [ low ] < f ) ? low ++ : low ; }