Given an array **arr[]** of non-negative integers of size **N**, the task is to find an integer **H** such that at least K integers in the array are greater or equal to K.**Examples:**

Input:arr[] = [3, 0, 6, 1, 5]Output:3Explanation:There are 3 number greater than or equal to 3 in the array i.e. 3, 6 and 5.

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

**Approach:** Using Hashing

The number integer K can not be greater than the size of arr[]. So, maintain the frequency of each element in a frequency array(hash table). Then traverse the frequency array from the end and return the first index that matches the condition.

Below is the implementation of the above approach.

## C++

`// C++ program for the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `getNumberK(vector<` `int` `>& S) ` `{ ` ` ` `// vector to store freq. ` ` ` `vector<` `int` `> freq(S.size() + 1, ` ` ` `0); ` ` ` ` ` `// Filling freq vector. ` ` ` `for` `(` `int` `i = 0; i < S.size(); ` ` ` `i++) { ` ` ` ` ` `if` `(S[i] < S.size()) ` ` ` `freq[S[i]]++; ` ` ` `else` ` ` `freq[S.size()]++; ` ` ` `} ` ` ` ` ` `int` `total = 0; ` ` ` ` ` `// Finding K number. ` ` ` `for` `(` `int` `i = S.size(); i >= 0; ` ` ` `i--) { ` ` ` ` ` `total += freq[i]; ` ` ` `if` `(total >= i) ` ` ` `return` `i; ` ` ` `} ` ` ` ` ` `// No K number found. ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr{ 3, 0, 6, 1, 5 }; ` ` ` `cout << getNumberK(arr) << ` `'\n'` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `static` `int` `getNumberK(ArrayList<Integer> S) ` `{ ` ` ` ` ` `// To store freq. ` ` ` `int` `[] freq = ` `new` `int` `[S.size() + ` `1` `]; ` ` ` ` ` `// Filling freq vector. ` ` ` `for` `(` `int` `i = ` `0` `; i < S.size(); i++) ` ` ` `{ ` ` ` `if` `(S.get(i) < S.size()) ` ` ` `freq[S.get(i)]++; ` ` ` `else` ` ` `freq[S.size()]++; ` ` ` `} ` ` ` ` ` `int` `total = ` `0` `; ` ` ` ` ` `// Finding K number. ` ` ` `for` `(` `int` `i = S.size(); i >= ` `0` `; i--) ` ` ` `{ ` ` ` `total += freq[i]; ` ` ` `if` `(total >= i) ` ` ` `return` `i; ` ` ` `} ` ` ` ` ` `// No K number found. ` ` ` `return` `0` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `ArrayList<Integer> arr = ` ` ` `new` `ArrayList<>(Arrays.asList(` `3` `, ` `0` `, ` `6` `, ` `1` `, ` `5` `)); ` ` ` ` ` `System.out.println(getNumberK(arr)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `def` `getNumberK(S): ` ` ` ` ` `# Vector to store freq. ` ` ` `freq ` `=` `[` `0` `] ` `*` `(` `len` `(S) ` `+` `1` `) ` ` ` ` ` `# Filling freq vector. ` ` ` `for` `i ` `in` `range` `(` `len` `(S)): ` ` ` `if` `(S[i] < ` `len` `(S)): ` ` ` `freq[S[i]] ` `+` `=` `1` ` ` `else` `: ` ` ` `freq[` `len` `(S)] ` `+` `=` `1` ` ` ` ` `total ` `=` `0` ` ` ` ` `# Finding K number. ` ` ` `for` `i ` `in` `range` `(` `len` `(S), ` `-` `1` `, ` `-` `1` `): ` ` ` `total ` `+` `=` `freq[i] ` ` ` ` ` `if` `(total >` `=` `i): ` ` ` `return` `i ` ` ` ` ` `# No K number found. ` ` ` `return` `0` ` ` `# Driver code ` `arr ` `=` `[ ` `3` `, ` `0` `, ` `6` `, ` `1` `, ` `5` `] ` ` ` `print` `(getNumberK(arr)) ` ` ` `# This code is contributed by code_hunt ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG{ ` ` ` `static` `int` `getNumberK(List<` `int` `> S) ` `{ ` ` ` ` ` `// To store freq. ` ` ` `int` `[] freq = ` `new` `int` `[S.Count + 1]; ` ` ` ` ` `// Filling freq vector. ` ` ` `for` `(` `int` `i = 0; i < S.Count; i++) ` ` ` `{ ` ` ` `if` `(S[i] < S.Count) ` ` ` `freq[S[i]]++; ` ` ` `else` ` ` `freq[S.Count]++; ` ` ` `} ` ` ` ` ` `int` `total = 0; ` ` ` ` ` `// Finding K number. ` ` ` `for` `(` `int` `i = S.Count; i >= 0; i--) ` ` ` `{ ` ` ` `total += freq[i]; ` ` ` `if` `(total >= i) ` ` ` `return` `i; ` ` ` `} ` ` ` ` ` `// No K number found. ` ` ` `return` `0; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `List<` `int` `> arr = ` `new` `List<` `int` `>{ 3, 0, 6, 1, 5 }; ` ` ` ` ` `Console.WriteLine(getNumberK(arr)); ` `} ` `} ` ` ` `// This code is contributed by Amit Katiyar ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity** O(N) **Space Complexity** O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.