LEARNING JOURNAL

I created this learning journal to practice writting and to help me learn by writting everything down. Source code here

2/1/2024

Number of Equal Numbers Blocks --- 4th attempt

This is my 4th attempt at solving this leetcode problem. I just recognized something interesting.

The basic idea of binary search is to look at the middle element to see if it is the thing that you are looking for. It it is, return that element. It it is not, compare that element to the element you are looking for to see if the unknown element is on the right or left side of the array, then continue to do the same thing to that side of the array until the upper bound and lower bound limits converge.

In this case, what we are looking for is an element that is neither equal nor different from the current element. You can never find that element, it does not exist. So, the loop will always stop in the middle, at the line between the two blocks. Then, by some miracle, the hi pointer moves 1 step further to the current block. That is why we need to return hi + 1.