fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. //Algo Used: Bucket Sort (Counting frequencies)
  8. // SC: O N, TC: O 1
  9. class Ideone {
  10. public static int hIndex(int[] citations) {
  11. int n= citations.length;
  12. 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
  13. for(int i=0; i< n; i++){
  14. int val= citations[i];
  15. if(val< n){ // if beyond size og arr then move it to last ind
  16. bucket[val]++;
  17. }else{
  18. bucket[n]++; // else count the freq
  19. }
  20. }
  21. for(int i=0;i<bucket.length;i++){
  22. System.out.println("i = "+ i +" val = " + bucket[i]);
  23.  
  24. }
  25. int count= 0;
  26. for(int i=n; i>=0; i--){ // from right side taking the first valid h-index which will be max as takig from right
  27. count+= bucket[i];
  28. if(i <= count){
  29. return i;
  30. }
  31. }
  32. return 0;
  33. }
  34. public static void main(String[] args){
  35. int[] arr = new int[]{3, 0, 6, 1, 5};
  36. int ans = hIndex(arr);
  37. System.out.println(ans);
  38. }
  39. }
Success #stdin #stdout 0.12s 57480KB
stdin
Standard input is empty
stdout
i = 0 val = 1
i = 1 val = 1
i = 2 val = 0
i = 3 val = 1
i = 4 val = 0
i = 5 val = 2
3