/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
//Algo Used: Bucket Sort (Counting frequencies)
// SC: O N, TC: O 1
class Ideone {
public static int hIndex(int[] citations) {
int n= citations.length;
int bucket[]= new int[n+1]; // storing the elements which are beyond the size of n in the n+1 extra space, bcz that can't be our h-idex
for(int i=0; i< n; i++){
int val= citations[i];
if(val< n){ // if beyond size og arr then move it to last ind
bucket[val]++;
}else{
bucket[n]++; // else count the freq
}
}
for(int i=0;i<bucket.length;i++){
System.
out.
println("i = "+ i
+" val = " + bucket
[i
]);
}
int count= 0;
for(int i=n; i>=0; i--){ // from right side taking the first valid h-index which will be max as takig from right
count+= bucket[i];
if(i <= count){
return i;
}
}
return 0;
}
public static void main
(String[] args
){ int[] arr = new int[]{3, 0, 6, 1, 5};
int ans = hIndex(arr);
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovL0FsZ28gVXNlZDogQnVja2V0IFNvcnQgKENvdW50aW5nIGZyZXF1ZW5jaWVzKQovLyBTQzogTyBOLCBUQzogTyAxCmNsYXNzIElkZW9uZSB7CiAgICBwdWJsaWMgc3RhdGljIGludCBoSW5kZXgoaW50W10gY2l0YXRpb25zKSB7CiAgICAgICAgaW50IG49IGNpdGF0aW9ucy5sZW5ndGg7CiAgICAgICAgaW50IGJ1Y2tldFtdPSBuZXcgaW50W24rMV07IC8vIHN0b3JpbmcgdGhlIGVsZW1lbnRzIHdoaWNoIGFyZSBiZXlvbmQgdGhlIHNpemUgb2YgbiBpbiB0aGUgbisxIGV4dHJhIHNwYWNlLCBiY3ogdGhhdCBjYW4ndCBiZSBvdXIgaC1pZGV4CiAgICAgICAgZm9yKGludCBpPTA7IGk8IG47IGkrKyl7IAogICAgICAgICAgICBpbnQgdmFsPSBjaXRhdGlvbnNbaV07CiAgICAgICAgICAgIGlmKHZhbDwgbil7IC8vIGlmIGJleW9uZCBzaXplIG9nIGFyciB0aGVuIG1vdmUgaXQgdG8gbGFzdCBpbmQKICAgICAgICAgICAgICAgIGJ1Y2tldFt2YWxdKys7CiAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgYnVja2V0W25dKys7ICAgIC8vIGVsc2UgY291bnQgdGhlIGZyZXEKICAgICAgICAgICAgfQogICAgICAgIH0KZm9yKGludCBpPTA7aTxidWNrZXQubGVuZ3RoO2krKyl7ClN5c3RlbS5vdXQucHJpbnRsbigiaSA9ICIrIGkgKyIgdmFsID0gIiArIGJ1Y2tldFtpXSk7Cgp9CiAgICAgICAgaW50IGNvdW50PSAwOwogICAgICAgIGZvcihpbnQgaT1uOyBpPj0wOyBpLS0peyAvLyBmcm9tIHJpZ2h0IHNpZGUgdGFraW5nIHRoZSBmaXJzdCB2YWxpZCBoLWluZGV4IHdoaWNoIHdpbGwgYmUgbWF4IGFzIHRha2lnIGZyb20gcmlnaHQKICAgICAgICAgICAgY291bnQrPSBidWNrZXRbaV07CiAgICAgICAgICAgIGlmKGkgPD0gY291bnQpewogICAgICAgICAgICAgICAgcmV0dXJuIGk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIDA7CiAgICB9CnB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpewppbnRbXSBhcnIgPSBuZXcgaW50W117MywgMCwgNiwgMSwgNX07CmludCBhbnMgPSBoSW5kZXgoYXJyKTsKU3lzdGVtLm91dC5wcmludGxuKGFucyk7Cn0KfQ==