I\'m trying to understand how do I modify the binary search for it work for first and last occurrences, surely I can find some code on the web but I\'m trying to reach deep
To my understanding, if the element you are looking for is found, one option would be to perform a linear search either to the left or right to find the first on last occurence.
Alternatively, you could use binary search again to adap the position until the element at the next position changes. This could be done by modifying the middle case above; if the element is found, don't return its position until the position remains the same.