🧠
Sorting + Binary Search on h
💡 Binary search on h-index values leverages the sorted array to find the maximum h efficiently, combining sorting and binary search skills.
Intuition
Sort citations descending. Use binary search on h from 0 to n. For each mid h, check if there are at least h papers with citations >= h. Narrow search accordingly.
Algorithm
- Sort citations in descending order.
- Set low = 0, high = n (number of papers).
- While low <= high:
- mid = (low + high) // 2
- Count how many papers have citations >= mid.
- If count >= mid, set low = mid + 1 (try higher h).
- Else set high = mid - 1 (try lower h).
- Return high as the maximum valid h.
💡 Binary search reduces the number of checks drastically by halving the search space each time, making it efficient for large inputs.
def hIndex(citations):
citations.sort(reverse=True)
n = len(citations)
low, high = 0, n
while low <= high:
mid = (low + high) // 2
count = 0
for c in citations:
if c >= mid:
count += 1
else:
break
if count >= mid:
low = mid + 1
else:
high = mid - 1
return high
# Driver code
if __name__ == '__main__':
print(hIndex([3,0,6,1,5])) # Output: 3
Line Notes
citations.sort(reverse=True)Sort descending to enable early break in counting
low, high = 0, nInitialize binary search boundaries for h
mid = (low + high) // 2Calculate middle h to test
if count >= midIf enough papers meet mid, try higher h
import java.util.Arrays;
public class Solution {
public static int hIndex(int[] citations) {
Arrays.sort(citations);
int n = citations.length;
int low = 0, high = n;
while (low <= high) {
int mid = (low + high) / 2;
int count = 0;
for (int i = n - 1; i >= 0; i--) {
if (citations[i] >= mid) count++;
else break;
}
if (count >= mid) low = mid + 1;
else high = mid - 1;
}
return high;
}
public static void main(String[] args) {
int[] citations = {3,0,6,1,5};
System.out.println(hIndex(citations)); // Output: 3
}
}
Line Notes
Arrays.sort(citations);Sort ascending to scan from end for descending order
int low = 0, high = n;Set binary search range for h
for (int i = n - 1; i >= 0; i--)Count papers with citations >= mid
if (count >= mid) low = mid + 1;Try higher h if condition holds
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int hIndex(vector<int>& citations) {
sort(citations.begin(), citations.end());
int n = citations.size();
int low = 0, high = n;
while (low <= high) {
int mid = (low + high) / 2;
int count = 0;
for (int i = n - 1; i >= 0; i--) {
if (citations[i] >= mid) count++;
else break;
}
if (count >= mid) low = mid + 1;
else high = mid - 1;
}
return high;
}
int main() {
vector<int> citations = {3,0,6,1,5};
cout << hIndex(citations) << endl; // Output: 3
return 0;
}
Line Notes
sort(citations.begin(), citations.end());Sort ascending to scan from end for descending order
int low = 0, high = n;Initialize binary search range
for (int i = n - 1; i >= 0; i--)Count papers with citations >= mid
if (count >= mid) low = mid + 1;Adjust low to search higher h
function hIndex(citations) {
citations.sort((a,b) => a - b);
const n = citations.length;
let low = 0, high = n;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
let count = 0;
for (let i = n - 1; i >= 0; i--) {
if (citations[i] >= mid) count++;
else break;
}
if (count >= mid) low = mid + 1;
else high = mid - 1;
}
return high;
}
// Test
console.log(hIndex([3,0,6,1,5])); // Output: 3
Line Notes
citations.sort((a,b) => a - b);Sort ascending to scan from end for descending order
let low = 0, high = n;Set binary search boundaries
for (let i = n - 1; i >= 0; i--)Count papers with citations >= mid
if (count >= mid) low = mid + 1;Try higher h if condition holds
TimeO(n log n)
SpaceO(1) or O(n) depending on sort
Sorting is O(n log n). Each binary search iteration counts papers in O(n). With O(log n) iterations, total O(n log n).
💡 For n=100000, this is efficient enough, but the linear count inside binary search can be optimized further.
Interview Verdict: Accepted
This approach is efficient and demonstrates mastery of binary search combined with sorting, a common interview pattern.