Given an array **arr[]** consisting of **N** non-negative integers, the task is to find an integer **K** for every index such that at least K integers in the array till that index are greater or equal to K.

**Note:** Consider 1 based indexing

**Examples:**

Input:arr[] = {3, 0, 6, 1, 5}

Output:K = {1, 1, 2, 2, 3}

Explanation:

At index 1, there is 1 number greater than or equal to 1 in the array i.e. 3. So K value for elements upto index 1 is 1.

At index 2, there is 1 number greater than or equal to 1 in the array i.e. 3. So K value for elements upto index 2 is 1.

At index 3, there are 2 numbers greater than or equal to 2 in the array i.e. 3 and 6. So K value for elements upto index 3 is 2.

At index 4, there are 2 numbers greater than or equal to 2 in the array i.e. 3 and 6. So K value for elements upto index 4 is 2.

At index 5, there are 3 numbers greater than or equal to 3 in the array i.e. 3, 6 and 5. So K value for elements up to index 5 is 3.

Input:arr[] = {9, 10, 7, 5, 0, 10, 2, 0}

Output:K = {1, 2, 3, 4, 4, 5, 5, 5}

**Naive Approach:**

The simplest approach is to find the value of **K** for all the elements of the array in the range **[0, i]**, where **i** is the index of the array **arr[]**, using the efficient approach used in the article whose link is given here.

**Time Complexity:** O(N^{2})

**Space Complexity:** O(N)

**Efficient Approach:**

The idea is to use Multiset(Red-Black Tree). Multiset stores the values in a sorted order which helps to check that if the current minimum value in the multiset is greater than or equal to its size. If yes, then the value of integer K will be the size of the multiset.

Below are the steps for the implementation:

- Traverse the array from index 0 to N-1.
- For each index, insert the element into the multiset and check if the smallest value in the multiset is less than the size of the multiset.
- If true, then erase the starting element and print the size of the multiset.
- If false then simply print the size of the multiset.
- The size of the multiset is the required K value for every index i.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the K-value ` `// for every index in the array ` `int` `print_h_index(` `int` `arr[], ` `int` `N) ` `{ ` ` ` `// Multiset to store the array ` ` ` `// in the form of red-black tree ` ` ` `multiset<` `int` `> ms; ` ` ` ` ` `// Iterating over the array ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` ` ` `// Inserting the current ` ` ` `// value in the multiset ` ` ` `ms.insert(arr[i]); ` ` ` ` ` `// Condition to check if ` ` ` `// the smallest value ` ` ` `// in the set is less than ` ` ` `// it's size ` ` ` `if` `(*ms.begin() ` ` ` `< ms.size()) { ` ` ` ` ` `// Erase the smallest ` ` ` `// value ` ` ` `ms.erase(ms.begin()); ` ` ` `} ` ` ` ` ` `// h-index value will be ` ` ` `// the size of the multiset ` ` ` `cout << ms.size() << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// array ` ` ` `int` `arr[] = { 9, 10, 7, 5, 0, ` ` ` `10, 2, 0 }; ` ` ` ` ` `// Size of the array ` ` ` `int` `N = ` `sizeof` `(arr) ` ` ` `/ ` `sizeof` `(arr[0]); ` ` ` ` ` `// function call ` ` ` `print_h_index(arr, N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

1 2 3 4 4 5 5 5

**Time Complexity:** O(N * log(N))

**Auxillary Space Complexity:** O(N)

## Recommended Posts:

- Find Array formed by adding each element of given array with largest element in new array to its left
- Find just strictly greater element from first array for each element in second array
- Count all prefixes of the given binary array which are divisible by x
- Find last element after deleting every second element in array of n integers
- Find an element in array such that sum of left array is equal to sum of right array
- Find element in array that divides all array elements
- Find if array has an element whose value is half of array sum
- Find closest value for every element in array
- Find extra element in the second array
- Find Second largest element in an array | Set 2
- Find the maximum element in the array other than A
_{i} - Find the maximum possible value of last element of the Array
- Find an element in Bitonic array
- Find Kth most occurring element in an Array
- Find Second largest element in an array
- Find the element whose multiplication with -1 makes array sum 0
- Find maximum sum taking every Kth element in the array
- Find the Mth element of the Array after K left rotations
- Find last remaining element after reducing the Array
- Find the position of the last removed element from the array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.