Java program for the above approach
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } public static int lower_bound ( int arr [ ] , int key ) { int l = - 1 , r = arr . length ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( arr [ m ] >= key ) r = m ; else l = m ; } return r ; } public static int upper_bound ( int arr [ ] , int key ) { int l = - 1 , r = arr . length ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; }