# Find H-Index for sorted citations using Binary Search

• Difficulty Level : Easy
• Last Updated : 16 Jul, 2021

Given an array citations[] consisting of N integers in non-increasing order, representing citations, the task is to find the H-index.

H-Index is usually assigned to the researcher denoting the contributions made in terms of no of papers and citations. H-index(H) is the largest value such that the researcher has at least H papers cited at least H times.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:

Input: citations[] = {5, 3, 3, 0, 0}
Output:
Explanation:
There are atleast 3 papers (5, 3, 3) with atleast 3 citations
Input: citations[] = {5, 4, 2, 1, 1}
Output:
Explanation:
There are atleast 2 papers (5, 4, 2) with atleast 2 citations.

Naive Approach: A simple solution is to iterate through the papers from left to right and increment the H-index while citationsi is greater than or equal to index.

Time Complexity: O(N)

Efficient Approach: The idea is to use binary search to optimize the above approach. The H-index can lie in the range from 0 to N. To check if a given value is possible or not, check if citations[value] is greater than or equal to value.

• Initialize the search range for the Binary search as 0 to N.
• Find the middle element of the range.
• Check if the middle element of the citation is less than the index. If so, then update the left range to middle element.
• Otherwise, check if the middle element of the citation is greater than the index. If so, then update the right range to the middle element.
• Otherwise, the given index is the H-index of the Citations.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the` `// above approach`   `#include ` `using` `namespace` `std;`   `// Function to find the H-index` `int` `hIndex(vector<``int``> citations,` `           ``int` `n)` `{`   `    ``int` `hindex = 0;`   `    ``// Set the range for binary search` `    ``int` `low = 0, high = n - 1;`   `    ``while` `(low <= high) {` `        ``int` `mid = (low + high) / 2;`   `        ``// Check if current citations is` `        ``// possible` `        ``if` `(citations[mid] >= (mid + 1)) {`   `            ``// Check to the right of mid` `            ``low = mid + 1;`   `            ``// Update h-index` `            ``hindex = mid + 1;` `        ``}` `        ``else` `{`   `            ``// Since current value is not` `            ``// possible, check to the left` `            ``// of mid` `            ``high = mid - 1;` `        ``}` `    ``}`   `    ``// Print the h-index` `    ``cout << hindex << endl;`   `    ``return` `hindex;` `}`   `// Driver Code` `int` `main()` `{`   `    ``// citations` `    ``int` `n = 5;` `    ``vector<``int``> citations = { 5, 3, 3, 2, 2 };`   `    ``hIndex(citations, n);` `}`

## Java

 `// Java implementation of the` `// above approach` `import` `java.io.*;`   `class` `GFG{`   `// Function to find the H-index` `static` `int` `hIndex(``int``[] citations, ``int` `n)` `{` `    ``int` `hindex = ``0``;`   `    ``// Set the range for binary search` `    ``int` `low = ``0``, high = n - ``1``;`   `    ``while` `(low <= high) ` `    ``{` `        ``int` `mid = (low + high) / ``2``;`   `        ``// Check if current citations is` `        ``// possible` `        ``if` `(citations[mid] >= (mid + ``1``))` `        ``{`   `            ``// Check to the right of mid` `            ``low = mid + ``1``;`   `            ``// Update h-index` `            ``hindex = mid + ``1``;` `        ``}` `        ``else` `        ``{`   `            ``// Since current value is not` `            ``// possible, check to the left` `            ``// of mid` `            ``high = mid - ``1``;` `        ``}` `    ``}`   `    ``// Print the h-index` `    ``System.out.println(hindex);`   `    ``return` `hindex;` `}`   `// Driver Code` `public` `static` `void` `main (String[] args)` `{`   `    ``// citations` `    ``int` `n = ``5``;` `    ``int``[] citations = { ``5``, ``3``, ``3``, ``2``, ``2` `};`   `    ``hIndex(citations, n);` `}` `}`   `// This code is contributed by sanjoy_62`

## Python3

 `# Python3 implementation of the ` `# above approach `   `# Function to find the H-index ` `def` `hIndex(citations, n):`   `    ``hindex ``=` `0`   `    ``# Set the range for binary search` `    ``low ``=` `0` `    ``high ``=` `n ``-` `1`   `    ``while` `(low <``=` `high):` `        ``mid ``=` `(low ``+` `high) ``/``/` `2`   `        ``# Check if current citations is` `        ``# possible` `        ``if` `(citations[mid] >``=` `(mid ``+` `1``)):`   `            ``# Check to the right of mid` `            ``low ``=` `mid ``+` `1`   `            ``# Update h-index` `            ``hindex ``=` `mid ``+` `1`   `        ``else``:` `            `  `            ``# Since current value is not` `            ``# possible, check to the left` `            ``# of mid` `            ``high ``=` `mid ``-` `1`   `    ``# Print the h-index` `    ``print``(hindex)`   `    ``return` `hindex`   `# Driver Code`   `# citations` `n ``=` `5` `citations ``=` `[ ``5``, ``3``, ``3``, ``2``, ``2` `]`   `# Function Call` `hIndex(citations, n)`   `# This code is contributed by Shivam Singh`

## C#

 `// C# implementation of the` `// above approach` `using` `System;`   `class` `GFG{`   `// Function to find the H-index` `static` `int` `hIndex(``int``[] citations, ``int` `n)` `{` `    ``int` `hindex = 0;`   `    ``// Set the range for binary search` `    ``int` `low = 0, high = n - 1;`   `    ``while` `(low <= high)` `    ``{` `        ``int` `mid = (low + high) / 2;`   `        ``// Check if current citations is` `        ``// possible` `        ``if` `(citations[mid] >= (mid + 1)) ` `        ``{` `            `  `            ``// Check to the right of mid` `            ``low = mid + 1;`   `            ``// Update h-index` `            ``hindex = mid + 1;` `        ``}` `        ``else` `        ``{` `            `  `            ``// Since current value is not` `            ``// possible, check to the left` `            ``// of mid` `            ``high = mid - 1;` `        ``}` `    ``}`   `    ``// Print the h-index` `    ``Console.WriteLine(hindex);`   `    ``return` `hindex;` `}`   `// Driver Code` `public` `static` `void` `Main ()` `{`   `    ``// citations` `    ``int` `n = 5;` `    ``int``[] citations = { 5, 3, 3, 2, 2 };`   `    ``hIndex(citations, n);` `}` `}`   `// This code is contributed by sanjoy_62`

## Javascript

 ``

Output:

`3`

Time Complexity: O(logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :