Function to calculate upper bound
public static int upperBound ( int [ ] array , int value ) { int low = 0 ; int high = array . Length ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; }