Find H-Index for sorted citations using Binary Search
Last Updated :
24 Feb, 2022
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.
Examples:
Input: citations[] = {5, 3, 3, 0, 0}
Output: 3
Explanation:
There are atleast 3 papers (5, 3, 3) with atleast 3 citations
Input: citations[] = {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++
#include <bits/stdc++.h>
using namespace std;
int hIndex(vector< int > citations,
int n)
{
int hindex = 0;
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (citations[mid] >= (mid + 1)) {
low = mid + 1;
hindex = mid + 1;
}
else {
high = mid - 1;
}
}
cout << hindex << endl;
return hindex;
}
int main()
{
int n = 5;
vector< int > citations = { 5, 3, 3, 2, 2 };
hIndex(citations, n);
}
|
Java
import java.io.*;
class GFG{
static int hIndex( int [] citations, int n)
{
int hindex = 0 ;
int low = 0 , high = n - 1 ;
while (low <= high)
{
int mid = (low + high) / 2 ;
if (citations[mid] >= (mid + 1 ))
{
low = mid + 1 ;
hindex = mid + 1 ;
}
else
{
high = mid - 1 ;
}
}
System.out.println(hindex);
return hindex;
}
public static void main (String[] args)
{
int n = 5 ;
int [] citations = { 5 , 3 , 3 , 2 , 2 };
hIndex(citations, n);
}
}
|
Python3
def hIndex(citations, n):
hindex = 0
low = 0
high = n - 1
while (low < = high):
mid = (low + high) / / 2
if (citations[mid] > = (mid + 1 )):
low = mid + 1
hindex = mid + 1
else :
high = mid - 1
print (hindex)
return hindex
n = 5
citations = [ 5 , 3 , 3 , 2 , 2 ]
hIndex(citations, n)
|
C#
using System;
class GFG{
static int hIndex( int [] citations, int n)
{
int hindex = 0;
int low = 0, high = n - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (citations[mid] >= (mid + 1))
{
low = mid + 1;
hindex = mid + 1;
}
else
{
high = mid - 1;
}
}
Console.WriteLine(hindex);
return hindex;
}
public static void Main ()
{
int n = 5;
int [] citations = { 5, 3, 3, 2, 2 };
hIndex(citations, n);
}
}
|
Javascript
<script>
function hIndex(citations, n)
{
let hindex = 0;
let low = 0, high = n - 1;
while (low <= high)
{
let mid = (low + high) / 2;
if (citations[mid] >= (mid + 1))
{
low = mid + 1;
hindex = mid + 1;
}
else
{
high = mid - 1;
}
}
document.write(hindex);
return hindex;
}
let n = 5;
let citations = [ 5, 3, 3, 2, 2 ];
hIndex(citations, n)
</script>
|
Time Complexity: O(logN)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...