It returns index of first element which is greater than searched value If searched element is bigger than any array element function returns first index after last element .
public static int upperBound ( ArrayList < Long > validNumbers , Long value ) { int low = 0 ; int high = validNumbers . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value >= validNumbers . get ( mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void answerQueries ( ArrayList < Query > queries ) {