Java implementation of above approach
import java . io . * ; import java . util . * ; class GFG { static int upperBound ( ArrayList < Integer > a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a . get ( middle ) > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; }