It takes me sometime to understand this question. Binary search is not difficult, the thing is how can we understand the meaning of c[m] < n - m and c[m] >= n - m, m is the middle index, n is the length of citations array, c is a abbreviation for citations array.

To understand this we should have a clear understanding of the origin solution to this problem. It is on wikipage: https://en.wikipedia.org/wiki/H-index

Summary here:

Sort the array descending order, give each a index start from 1.

From right to left, find the last number >= its index, the result is its index.

....c: 25, 8, 5, 3, 3

index:1, 2, 3, 4, 5

number 5, H-index 3.

After understand this origin solution, we can go to the binary search one.

First, the different is the order changed, in this problem, the array sorted in ascending.

We need somehow transfer it to original problem.

For example, we have those number, and their index starts with 0

......c: 3, 3, 5, 8, 25

index: 0, 1, 2, 3, 4

We can covert it using n the length of the array. We subtract n with the index, we get:

........c: 3, 3, 5, 8, 25

index0: 0, 1, 2, 3, 4

index1: 5, 4, 3, 2, 1

We can see we almost have the original form now except the order. It is easy, in the original problem we try to find the last one >= its index, now we find the first one >= its index.

So now we just using binary search to find the number, a thing here we need to mind is the index0 here we are using, but we need to convert it to index1.

public int hIndex(int[] c) { int n = c.length; int s = 0, e = n - 1; while(s < e){ int m = (s + e) / 2; if(c[m] < n - m){ s = m + 1; } else { e = m; } } if(s < n && c[s] >= n - s) return n - s; else return 0; }