Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

Leetcode H-Index problem solution

YASH PAL, 31 July 2024

In this Leetcode H-Index problem solution, we have given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper, return compute the researcher’s h-index.

According to the definition of h-index on Wikipedia: A scientist has an 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.

If there are several possible values for h, the maximum one is taken as the h-index.

Leetcode H-Index problem solution

Topics we are covering

Toggle
  • Problem solution in Python.
  • Problem solution in Java.
  • Problem solution in C++.
  • Problem solution in C.

Problem solution in Python.

class Solution:
    def hIndex(self, citations: List[int]) -> int:
        if not citations:
            return 0
        citations.sort()
        h = len(citations)
        if citations[0] >= h:
            return h
        else:
            for i in range(1, len(citations)+1):
                if h <= citations[i-1]:
                    return h
                h -= 1
        return h

Problem solution in Java.

public int hIndex(int[] citations) {
        Arrays.sort(citations);
        int h = 0;
        for(int i = citations.length - 1; i >= 0; i--){
            if(citations[i] < h + 1) break;
            h += 1;
        }
        return h;
    }

Problem solution in C++.

class Solution {
public:
int hIndex(vector<int>& citations) {
    int n=citations.size();
    if(n==0)
        return 0;
        
    sort(citations.begin(),citations.end());
    int res=0;
    for(int i=0;i<n;i++){
        if(citations[i]>=n-i)
            res=max(res,n-i);
    }
    return res;
}
};

Problem solution in C.

int partition(int a[], int p, int r) {
    int temp = 0;
    int i = p;
    int j = p;
    int pivot = a[r];
    
    for (j = p; j < r; j++) {
        if (a[j] > pivot) {
            temp = a[j];
            a[j] = a[i];
            a[i] = temp;
            i++;
        }
    }
    
    temp = a[r];
    a[r] = a[i];
    a[i] = temp;
    
    return i;
}

void quickSort(int a[], int p, int r) {
    if (p >= r) {
        return;
    }
    
    int q = partition(a, p, r);
    quickSort(a, p, q - 1);
    quickSort(a, q + 1, r);
}

int hIndex(int* citations, int citationsSize) {
    quickSort(citations, 0, citationsSize - 1);
    
    int i = 0;
    int ret = 0;
    for (i = 0; i < citationsSize; i++) {
        if (citations[i] >= i + 1) {
            ret = i + 1 > ret ? i + 1 : ret;
        }
    }
    
    return ret;
}

coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes