Given an array arr[] 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.
Examples:
Input: arr[] = {5, 3, 3, 0, 0}
Output: 3
Explanation:
There are atleast 3 papers (5, 3, 3) with atleast 3 citationsInput: arr[] = {5, 4, 2, 1, 1}
Output: 2
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 <bits/stdc++.h> 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 = 5citations = [ 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 |
3
Time Complexity: O(logN)
Auxiliary Space: O(1)
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.
Recommended Posts:
- Meta Binary Search | One-Sided Binary Search
- Create a Sorted Array Using Binary Search
- Check if an array is sorted and rotated using Binary Search
- Interpolation search vs Binary search
- Why is Binary Search preferred over Ternary Search?
- Linear Search vs Binary Search
- Binary search in sorted vector of pairs
- Find square root of number upto given precision using binary search
- Anagram Substring Search (Or Search for all permutations)
- Sublist Search (Search a linked list in another list)
- Best First Search (Informed Search)
- Repeatedly search an element by doubling it after every successful search
- Search an element in a sorted and rotated array
- Search in a row wise and column wise sorted matrix
- Search in an almost sorted array
- Search in an array of strings where non-empty strings are sorted
- Search, insert and delete in a sorted array
- Search element in a sorted matrix
- Search equal, bigger or smaller in a sorted array in Java
- Search in a sorted 2D matrix (Stored in row major order)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

