Find H | C ++ implementation of the above approach ; Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Print the h - index ; Driver Code ; citations
#include <bits/stdc++.h> NEW_LINE 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 ) ; }