If middle value is less than or equal to val then index can lie in mid + 1. . n else it lies in 0. . mid - 1.
while ( l <= h ) { mid = ( l + h ) / 2 ; if ( preSum [ mid ] . first <= val ) { ans = mid ; l = mid + 1 ; } else h = mid - 1 ; } return ans ; }