📌  相关文章
📜  最大值 K 使得数组至少有 K 个 >= K 的元素

📅  最后修改于: 2022-05-13 01:57:47.286000             🧑  作者: Mango

最大值 K 使得数组至少有 K 个 >= K 的元素

给定一个整数数组,找到最大可能值 K,使得该数组至少有 K 个大于或等于 K 的元素。该数组未排序并且可能包含重复值。
例子 :

Input: [2, 3, 4, 5, 6, 7]
Output: 4
Explanation : 4 elements [4, 5, 6, 7] 
            are greater than equal to 4

Input: [1, 2, 3, 4]
Output: 2
Explanation : 3 elements [2, 3, 4] are 
               greater than equal to 2

Input: [4, 7, 2, 3, 8]
Output: 3 
Explanation : 4 elements [4, 7, 3, 8] 
          are greater than equal to 3
 

Input: [6, 7, 9, 8, 10]
Output: 5
Explanation : All 5 elements are greater
              than equal to 5 

预期时间复杂度:O(n)


方法 1 [简单:O(n 2 ) 时间]
设输入数组的大小为 n。让我们考虑以下重要的观察。

  1. 结果的最大可能值可以是n。当所有元素都大于或等于 n 时,我们得到最大可能值。例如输入数组为{10, 20, 30},n为3。result的值不能大于3。
  2. 最小可能值为 1。获得此输出的一个示例是,当所有元素为 1 时。

所以我们可以运行一个从 n 到 1 的循环,并为每个值计算更大的元素。

C++
// C++ program to find maximum possible value K
// such that array has at-least K elements that
// are greater than or equals to K.
#include 
using namespace std;
 
// Function to return maximum possible value K
// such that array has atleast K elements that
// are greater than or equals to K
int findMaximumNum(unsigned int arr[], int n)
{
    // output can contain any number from n to 0
    // where n is length of the array
 
    // We start a loop from n as we need to find
    // maximum possible value
    for (int i = n; i >= 1; i--)
    {
        // count contains total number of elements
        // in input array that are more than equal to i
        int count = 0;
 
        // traverse the input array and find count
        for (int j=0; j= i)
          return i;
    }   
    return 1;
}
 
// Driver code
int main()
{
    unsigned int arr[] = {1, 2, 3, 8, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << findMaximumNum(arr, n);
    return 0;
}


Java
// Java program to find maximum
// possible value K such that
// array has at-least K elements
// that are greater than or equals to K.
import java.io.*;
 
class GFG
{
 
// Function to return maximum
// possible value K such that
// array has atleast K elements
// that are greater than or equals to K
static int findMaximumNum(int arr[],
                          int n)
{
    // output can contain any
    // number from n to 0 where
    // n is length of the array
 
    // We start a loop from n
    // as we need to find
    // maximum possible value
    for (int i = n; i >= 1; i--)
    {
        // count contains total
        // number of elements
        // in input array that
        // are more than equal to i
        int count = 0;
 
        // traverse the input
        // array and find count
        for (int j = 0; j < n; j++)
            if (i <= arr[j])
                count++;
 
        if (count >= i)
        return i;
    }
    return 1;
}
 
// Driver code
public static void main (String[] args)
{
int arr[] = {1, 2, 3, 8, 10 };
int n = arr.length;
System.out.println(findMaximumNum(arr, n));
}
}
 
// This code is contributed by aj_36


Python3
# python 3 program to find maximum possible value K
# such that array has at-least K elements that
# are greater than or equals to K.
 
# Function to return maximum possible value K
# such that array has atleast K elements that
# are greater than or equals to K
def findMaximumNum(arr, n):
    # output can contain any number from n to 0
    # where n is length of the array
 
    # We start a loop from n as we need to find
    # maximum possible value
    i = n
    while(i >= 1):
        # count contains total number of elements
        # in input array that are more than equal to i
        count = 0
 
        # traverse the input array and find count
        for j in range(0,n,1):
            if (i <= arr[j]):
                count += 1
 
        if (count >= i):
            return i
             
        i -= 1
     
    return 1
 
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3, 8, 10]
    n = len(arr)
    print(findMaximumNum(arr, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to find maximum
// possible value K such that
// array has at-least K elements
// that are greater than or equals to K.
using System;
 
class GFG
{
     
// Function to return maximum
// possible value K such that
// array has atleast K elements
// that are greater than or equals to K
static int findMaximumNum(int []arr,
                          int n)
{
    // output can contain any
    // number from n to 0 where
    // n is length of the array
 
    // We start a loop from n
    // as we need to find
    // maximum possible value
    for (int i = n; i >= 1; i--)
    {
        // count contains total
        // number of elements
        // in input array that
        // are more than equal to i
        int count = 0;
 
        // traverse the input
        // array and find count
        for (int j = 0; j < n; j++)
            if (i <= arr[j])
                count++;
 
        if (count >= i)
        return i;
    }
    return 1;
}
 
// Driver code
static public void Main ()
{
    int []arr = {1, 2, 3, 8, 10 };
    int n = arr.Length;
    Console.WriteLine(findMaximumNum(arr, n));
}
}
 
// This code is contributed by m_kit


PHP
= 1; $i--)
    {
        // count contains total
        // number of elements in
        // input array that are
        // more than equal to i
        $count = 0;
 
        // traverse the input
        // array and find count
        for ($j = 0; $j < $n; $j++)
            if ($i <= $arr[$j])
                $count++;
 
        if ($count >= $i)
        return $i;
    }
    return 1;
}
 
// Driver code
$arr = array (1, 2, 3, 8, 10);
$n = sizeof($arr);
echo findMaximumNum($arr, $n);
 
// This code is contributed by ajit
?>


Javascript


C++
// C++ program to find maximum possible value K such
// that array has atleast K elements that are greater
// than or equals to K.
#include 
using namespace std;
 
// Function to return maximum possible value K such
// that array has at-least K elements that are greater
// than or equals to K.
int findMaximumNum(unsigned int arr[], int n)
{
    // construct auxiliary array of size n + 1 and
    // initialize the array with 0
    vector freq(n+1, 0);
 
    // store the frequency of elements of
    // input array in the auxiliary array
    for (int i = 0; i < n; i++)
    {
        // If element is smaller than n, update its
        // frequency
        if (arr[i] < n)
            freq[arr[i]]++;
 
        // Else increment count of elements greater
        // than n.
        else
            freq[n]++;
    }
 
    // sum stores number of elements in input array
    // that are greater than or equal to current
    // index
    int sum = 0;
 
    // scan auxiliary array backwards
    for (int i = n; i > 0; i--)
    {
        sum += freq[i];
 
        // if sum is greater than current index,
        // current index is the answer
        if (sum >= i)
            return i;
    }
}
 
// Driver code
int main()
{
    unsigned int arr[] = {1, 2, 3, 8, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findMaximumNum(arr, n);
 
    return 0;
}


Java
// Java program to find maximum possible value K such
// that array has atleast K elements that are greater
// than or equals to K.
 
import java.util.Vector;
 
class GFG {
 
// Function to return maximum possible value K such
// that array has at-least K elements that are greater
// than or equals to K.
    static int findMaximumNum(int arr[], int n) {
        // construct auxiliary array of size n + 1 and
        // initialize the array with 0
        int[] freq=new int[n+1];
        for (int i = 0; i < n + 1; i++) {
            freq[i] = 0;
        }
 
        // store the frequency of elements of
        // input array in the auxiliary array
        for (int i = 0; i < n; i++) {
            // If element is smaller than n, update its
            // frequency
            if (arr[i] < n) //
            {
                freq[arr[i]]++;
            } // Else increment count of elements greater
            // than n.
            else {
                freq[n]++;
            }
        }
 
        // sum stores number of elements in input array
        // that are greater than or equal to current
        // index
        int sum = 0;
 
        // scan auxiliary array backwards
        for (int i = n; i > 0; i--) {
            sum += freq[i];
 
            // if sum is greater than current index,
            // current index is the answer
            if (sum >= i) {
                return i;
            }
        }
        return 0;
    }
 
// Driver code
    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 8, 10};
        int n = arr.length;
        System.out.println(findMaximumNum(arr, n));
    }
}
/*This Java code is contributed by koulick_sadhu*/


C#
// C# program to find maximum possible value K such
// that array has atleast K elements that are greater
// than or equals to K.
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to return maximum possible value K such
    // that array has at-least K elements that are greater
    // than or equals to K.
    static int findMaximumNum(int []arr, int n)
    {
        // construct auxiliary array of size n + 1 and
        // initialize the array with 0
        List freq = new List();
        for (int i = 0; i < n + 1; i++)
        {
            freq.Insert(i, 0);
        }
 
        // store the frequency of elements of
        // input array in the auxiliary array
        for (int i = 0; i < n; i++)
        {
            // If element is smaller than n, update its
            // frequency
            if (arr[i] < n) //freq[arr[i]]++;
            {
                freq.Insert(arr[i], freq[arr[i]] + 1);
            }
            // Else increment count of elements greater
            // than n.
            else
            {
                freq.Insert(n, freq[n] + 1);
            }
            //freq[n]++;
        }
 
        // sum stores number of elements in input array
        // that are greater than or equal to current
        // index
        int sum = 0;
 
        // scan auxiliary array backwards
        for (int i = n; i > 0; i--)
        {
            sum += freq[i];
 
            // if sum is greater than current index,
            // current index is the answer
            if (sum >= i)
            {
                return i;
            }
        }
        return 0;
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = {1, 2, 3, 8, 10};
        int n = arr.Length;
        Console.WriteLine(findMaximumNum(arr, n));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Javascript


输出 :

3

方法 2 [高效:O(n) 时间和 O(n) 额外空间]
1) 这个想法是构造大小为 n + 1 的辅助数组,并使用该数组来查找输入数组中更大元素的计数。设辅助数组为freq[]。我们将此数组的所有元素初始化为 0。
2)我们处理所有输入元素。
a) 如果一个元素 arr[i] 小于 n,那么我们增加它的频率,即我们做 freq[arr[i]]++。
b) 否则我们增加freq[n]。
3)在第 2 步之后,我们有两件事。
a) 存储在 freq[0..n-1] 中的小于 n 的元素的频率。
b) 存储在 freq[n] 中的大于 n 的元素计数。
最后,我们向后处理 freq[] 数组以通过保持目前处理的值的总和来找到输出。
下面是上述想法的实现。

C++

// C++ program to find maximum possible value K such
// that array has atleast K elements that are greater
// than or equals to K.
#include 
using namespace std;
 
// Function to return maximum possible value K such
// that array has at-least K elements that are greater
// than or equals to K.
int findMaximumNum(unsigned int arr[], int n)
{
    // construct auxiliary array of size n + 1 and
    // initialize the array with 0
    vector freq(n+1, 0);
 
    // store the frequency of elements of
    // input array in the auxiliary array
    for (int i = 0; i < n; i++)
    {
        // If element is smaller than n, update its
        // frequency
        if (arr[i] < n)
            freq[arr[i]]++;
 
        // Else increment count of elements greater
        // than n.
        else
            freq[n]++;
    }
 
    // sum stores number of elements in input array
    // that are greater than or equal to current
    // index
    int sum = 0;
 
    // scan auxiliary array backwards
    for (int i = n; i > 0; i--)
    {
        sum += freq[i];
 
        // if sum is greater than current index,
        // current index is the answer
        if (sum >= i)
            return i;
    }
}
 
// Driver code
int main()
{
    unsigned int arr[] = {1, 2, 3, 8, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findMaximumNum(arr, n);
 
    return 0;
}

Java

// Java program to find maximum possible value K such
// that array has atleast K elements that are greater
// than or equals to K.
 
import java.util.Vector;
 
class GFG {
 
// Function to return maximum possible value K such
// that array has at-least K elements that are greater
// than or equals to K.
    static int findMaximumNum(int arr[], int n) {
        // construct auxiliary array of size n + 1 and
        // initialize the array with 0
        int[] freq=new int[n+1];
        for (int i = 0; i < n + 1; i++) {
            freq[i] = 0;
        }
 
        // store the frequency of elements of
        // input array in the auxiliary array
        for (int i = 0; i < n; i++) {
            // If element is smaller than n, update its
            // frequency
            if (arr[i] < n) //
            {
                freq[arr[i]]++;
            } // Else increment count of elements greater
            // than n.
            else {
                freq[n]++;
            }
        }
 
        // sum stores number of elements in input array
        // that are greater than or equal to current
        // index
        int sum = 0;
 
        // scan auxiliary array backwards
        for (int i = n; i > 0; i--) {
            sum += freq[i];
 
            // if sum is greater than current index,
            // current index is the answer
            if (sum >= i) {
                return i;
            }
        }
        return 0;
    }
 
// Driver code
    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 8, 10};
        int n = arr.length;
        System.out.println(findMaximumNum(arr, n));
    }
}
/*This Java code is contributed by koulick_sadhu*/

C#

// C# program to find maximum possible value K such
// that array has atleast K elements that are greater
// than or equals to K.
using System;
using System.Collections.Generic;
 
class GFG
{
 
    // Function to return maximum possible value K such
    // that array has at-least K elements that are greater
    // than or equals to K.
    static int findMaximumNum(int []arr, int n)
    {
        // construct auxiliary array of size n + 1 and
        // initialize the array with 0
        List freq = new List();
        for (int i = 0; i < n + 1; i++)
        {
            freq.Insert(i, 0);
        }
 
        // store the frequency of elements of
        // input array in the auxiliary array
        for (int i = 0; i < n; i++)
        {
            // If element is smaller than n, update its
            // frequency
            if (arr[i] < n) //freq[arr[i]]++;
            {
                freq.Insert(arr[i], freq[arr[i]] + 1);
            }
            // Else increment count of elements greater
            // than n.
            else
            {
                freq.Insert(n, freq[n] + 1);
            }
            //freq[n]++;
        }
 
        // sum stores number of elements in input array
        // that are greater than or equal to current
        // index
        int sum = 0;
 
        // scan auxiliary array backwards
        for (int i = n; i > 0; i--)
        {
            sum += freq[i];
 
            // if sum is greater than current index,
            // current index is the answer
            if (sum >= i)
            {
                return i;
            }
        }
        return 0;
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = {1, 2, 3, 8, 10};
        int n = arr.Length;
        Console.WriteLine(findMaximumNum(arr, n));
    }
}
 
/* This code contributed by PrinciRaj1992 */

Javascript


输出 :

3