📌  相关文章
📜  找到一个数字 K 使得 Array 至少包含 K 个大于或等于 K 的数字

📅  最后修改于: 2021-09-04 13:07:02             🧑  作者: Mango

给定一个大小为N的非负整数数组arr[] ,任务是找到一个整数H ,使得数组中至少有 K 个整数大于或等于 K。
例子:

方法:使用哈希
数字整数 K 不能大于 arr[] 的大小。因此,维护频率数组(哈希表)中每个元素的频率。然后从末尾开始遍历频率数组,返回第一个符合条件的索引。
下面是上述方法的实现。

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
int getNumberK(vector& S)
{
    // vector to store freq.
    vector 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 arr{ 3, 0, 6, 1, 5 };
    cout << getNumberK(arr) << '\n';
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
static int getNumberK(ArrayList 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 arr =
          new ArrayList<>(Arrays.asList(3, 0, 6, 1, 5));
                                                
    System.out.println(getNumberK(arr));
}
}
 
// This code is contributed by offbeat


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


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
static int getNumberK(List 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 arr = new List{ 3, 0, 6, 1, 5 };
                                                 
    Console.WriteLine(getNumberK(arr));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
3

时间复杂度O(N)
空间复杂度O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live