
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find H-Index from a list of citations in C++
Suppose we have an array of citations of a researcher. We have to define a function to compute the researcher's h-index. As we know the h-index is a metric used to calculate the impact of a researcher's papers. Formally H-index can be defined as: "A researcher has index h if h of their N papers have at least h citations each, and the other N − h papers have no more than h citations each."
So, if the input is like citations = [5, 4, 1, 2, 6], then the output will be 3, as at least 3 papers have at least 3 citations each − 4, 5, 6.
To solve this, we will follow these steps −
- n := size of the array, create one array called bucket of size n + 1
- for i in range 0 to n – 1
- x := c[i]
- if x >= n, then increase bucket[n] by 1, otherwise increase bucket[x] by 1
- cnt := 0
- for i in range n down to 0:
- increase cnt by bucket[i]
- if cnt >= i, then return i
- return – 1
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: int solve(vector<int>& c) { int n = c.size(); vector <int> bucket(n + 1); for(int i = 0; i < n; i++){ int x = c[i]; if(x >= n){ bucket[n]++; } else { bucket[x]++; } } int cnt = 0; for(int i = n; i >= 0; i--){ cnt += bucket[i]; if(cnt >= i)return i; } return -1; } }; main(){ Solution ob; vector<int> v = {5, 4, 1, 2, 6}; cout << (ob.solve(v)); }
Input
[5, 4, 1, 2, 6]
Output
3
- Related Articles
- C# program to find the index of an element in a List
- Program to find list that shows closest distance of character c from that index in Python
- Program to find largest kth index value of one list in Python
- Program to find a list of product of all elements except the current index in Python
- Calculating h index of a citation in JavaScript
- Program to find folded list from a given linked list in Python
- H-Index in C++
- How to find the index of a list item in Swift?
- Program to find duplicate item from a list of elements in Python
- How to remove index list from another list in python?
- Program to find total unique duration from a list of intervals in Python
- Program to find first fit room from a list of rooms in Python
- Program to find number of arithmetic sequences from a list of numbers in Python?
- Program to find number of arithmetic subsequences from a list of numbers in Python?
- Program to find length of longest interval from a list of intervals in Python

Advertisements