Swap L and R if R is smaller than L
if ( R < L ) { long temp = L ; L = R ; R = temp ; } if ( L == 0 ) { int indxOfLastNum = upperBound ( validNumbers , R ) ; System . out . println ( indxOfLastNum + 1 ) ; } else { int indxOfFirstNum = upperBound ( validNumbers , L ) ; int indxOfLastNum = upperBound ( validNumbers , R ) ; System . out . println ( ( indxOfLastNum - indxOfFirstNum + 1 ) ) ; } } } public static void main ( String [ ] args ) { int Q = 2 ; ArrayList < Query > queries = new ArrayList < > ( ) ; Query q1 = new Query ( ) ; q1 . L = 3 ; q1 . R = 7 ; Query q2 = new Query ( ) ; q2 . L = 10 ; q2 . R = 16 ; queries . add ( q1 ) ; queries . add ( q2 ) ; answerQueries ( queries ) ; } }