Find K for every Array element such that at least K prefixes are â ‰¥ K | Java program for the above approach ; Function to find the K - value for every index in the array ; Multiset to store the array in the form of red - black tree ; Iterating over the array ; Inserting the current value in the multiset ; Condition to check if the smallest value in the set is less than it 's size ; Erase the smallest value ; h - index value will be the size of the multiset ; Driver code ; Array ; Size of the array ; Function call
import java . util . * ; class GFG { static void print_h_index ( int arr [ ] , int N ) { List < Integer > ms = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { ms . add ( arr [ i ] ) ; int t = Collections . min ( ms ) ; if ( t < ms . size ( ) ) { ms . remove ( ms . indexOf ( t ) ) ; } System . out . print ( ms . size ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 10 , 7 , 5 , 0 , 10 , 2 , 0 } ; int N = arr . length ; print_h_index ( arr , N ) ; } }