@Jerry has been explaining very detailed, thanks!

Here I'm writing down my understanding based on Jerry's post to help those who are still confused about "why binary search".

Before we dive into binary search, let's understand the original solution from wiki first.

We have a descending-sorted array, citations[] and base-1 index of them.

citations[i] 25, 8, 5, 3, 3 index[i]: 1, 2, 3, 4, 5It's hard to think it over with just the concept "index". Let's change to another meaning.

The index of citations[i] simply means the number of papers whose citations equal or larger than citations[i]

i.e. There are index[i] papers which have been cited at least citations[i] times.

With this definition in mind, we can go further.

For a certain i, say it's corresponding to index[i] = x and citations[i] = n. Then we have x papers which have citations number >= n.

If n >= x, we will have x papers which have citations number >= n >= x.

See now x will be a candidate for h-index.

How to find the max candidate? Find from right to left until we first come across some n >= x, i.e. citations[i] >= index[i].

This explains thinking detail of original solution. Now let's focus on binary search.

What does binary search do? To find an element that meets some equality constraints. In this problem, we are searching citations[i] >= index[i] (conversion from 0-base index to 1-base see Jerry's post). If we are using standard binary search(wiki version) we have the same as @dong-wang-1694 's post:

int left=0, len = citations.size(), right= len-1, mid; while(left<=right) { mid=(left+right)>>1; if(citations[mid] == (len-mid)) return citations[mid]; else if(citations[mid] > (len-mid)) right = mid - 1; else left = mid + 1; } return len - (right+1);An example problem:

citations[i]: 0, 1, 3, 5, 7 base-0 index: 0, 1, 2, 3, 4 base-1 index: 5, 4, 3, 2, 1It's easy to understand case:citations[mid] == (len-mid) directly returns the result.

Then what if instead of 0, 1, 3, 5, 7 , we have 0, 1, 4, 5, 7 ?

The algorithm will jump out op while loop. We know for binary search, if it cannot find the target, pointers left and right will be right besides the location which should be the target.

left v 0, 1, 4, 5, 7 ^ rightLet's imagine there is a virtual candidate of h-index which is 3.5, and insert it into original array.

citations[i]: 0, 1, (3.5), 4, 5, 7 base-1 index: 5, 4, (3.5), 3, 2, 1 base-0 index: 0, 1, 2, 3, 4Thus from left to right to find the first i so that citations[i] >= base-1 index[i], we have left pointer pointing the result. And its 1-based index , i.e. len - left = len - (right+1) is the final answer.

Hope this post can help.