Finding the upper and lower bound using binary search
I am trying to understand what aspect of my code could be made more efficient and faster.


First, the low hanging fruits...


You unnecessarily copy the sequence of integers when passing it to findBound:

int findBound(vector&lt;int&gt; a, int b, bool lower)


should be

int findBound(const vector&lt;int&gt;&amp; a, int b, bool lower)

You dispatch on the bool lower flag in every iteration of the main while-loop:

if(lower) {
   /* ... */
} else {
   /* ... */
}


Consider implementing two separate functions for the starting and one for the end index of the range.
In one of the if-conditions in the middle of the main loop, you compute a.size() - 1. This could be done once at the top of the function and bound to a const-qualified variable. No need to evaluate this in every iteration.


Now, the crucial step...


You are doing unnecessary work when comparing values. The very first if-condition,

if(a[mid]==b) { // ...


tests for the branch with the least likelihood. Instead, check for a[mid] &lt; b and nothing more. If you're wondering how this can be sufficient, check out this part of Sean Parent's Pacific C++ talk from 2018.