给定大小为N的数组a [] ,该数组仅包含非负元素,任务是找到任何整数K,对于该整数K恰好存在大于或等于K的K个数组元素。如果不存在这样的K ,则打印-1 。
例子:
Input: a[] = {7, 8, 9, 0, 0, 1}
Output: 3
Explanation:
Since 3 is less than or equal to 7, 8, and 9, therefore, 3 is the answer.
Input: a[] = {0, 0}
Output: -1
方法:任务是找到K使得数组元素大于或等于K。因此, K不能超过数组a [n]中存在的最大元素。请按照以下步骤解决问题:
- 遍历数组以找到最大的数组元素,并将其存储在变量中,例如m 。
- 初始化计数器变量cnt,以计算大于或等于K的数组元素的数量。
- 迭代从0到m的K可能值。为每个值迭代数组,并计算大于或等于该值的数组元素的数量。
- 如果对于任何值,发现恰好有K个数组元素大于或等于该值,则打印该值。
- 如果在数组完全遍历之后没有获得此类值,请打印-1 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find K for which
// there are exactly K array
// elements greater than or equal to K
int zvalue(vector& nums)
{
// Finding the largest array element
int m = *max_element(nums.begin(),
nums.end());
int cnt = 0;
// Possible values of K
for (int i = 0; i <= m; i++) {
cnt = 0;
// Traverse the array
for (int j = 0; j < nums.size(); j++) {
// If current array element is
// greater than or equal to i
if (nums[j] >= i)
cnt++;
}
// If i array elements are
// greater than or equal to i
if (cnt == i)
return i;
}
// Otherwise
return -1;
}
// Driver Code
int main()
{
vector nums = { 7, 8, 9, 0, 0, 1 };
cout << zvalue(nums) << endl;
}
Java
// Java program for the above approach
import java.io.*;
class GFG{
// Function to find K for which
// there are exactly K array
// elements greater than or equal to K
public static int zvalue(int[] nums)
{
// Finding the largest array element
int m = max_element(nums);
int cnt = 0;
// Possible values of K
for(int i = 0; i <= m; i++)
{
cnt = 0;
// Traverse the array
for(int j = 0; j < nums.length; j++)
{
// If current array element is
// greater than or equal to i
if (nums[j] >= i)
cnt++;
}
// If i array elements are
// greater than or equal to i
if (cnt == i)
return i;
}
// Otherwise
return -1;
}
// To find maximum Element
public static int max_element(int[] nums)
{
int max = nums[0];
for(int i = 1; i < nums.length; i++)
max = Math.max(max, nums[i]);
return max;
}
// Driver Code
public static void main(String args[])
{
int[] nums = { 7, 8, 9, 0, 0, 1 };
System.out.println(zvalue(nums));
}
}
// This code is contributed by hemanth gadarla
Python3
# Python3 program for the above approach
# Function to find K for which
# there are exactly K array
# elements greater than or equal to K
def zvalue(nums):
# Finding the largest array element
m = max(nums)
cnt = 0
# Possible values of K
for i in range(0, m + 1, 1):
cnt = 0
# Traverse the array
for j in range(0, len(nums), 1):
# If current array element is
# greater than or equal to i
if (nums[j] >= i):
cnt += 1
# If i array elements are
# greater than or equal to i
if (cnt == i):
return i
# Otherwise
return -1
# Driver Code
if __name__ == '__main__':
nums = [ 7, 8, 9, 0, 0, 1 ]
print(zvalue(nums))
# This code is contributed by SURENDRA_GANGWAR
C#
// C# program for the
// above approach
using System;
class GFG{
// Function to find K for which
// there are exactly K array
// elements greater than or equal to K
public static int zvalue(int[] nums)
{
// Finding the largest array element
int m = max_element(nums);
int cnt = 0;
// Possible values of K
for(int i = 0; i <= m; i++)
{
cnt = 0;
// Traverse the array
for(int j = 0;
j < nums.Length; j++)
{
// If current array element is
// greater than or equal to i
if (nums[j] >= i)
cnt++;
}
// If i array elements are
// greater than or equal to i
if (cnt == i)
return i;
}
// Otherwise
return -1;
}
// To find maximum Element
public static int max_element(int[] nums)
{
int max = nums[0];
for(int i = 1; i < nums.Length; i++)
max = Math.Max(max, nums[i]);
return max;
}
// Driver Code
public static void Main(String []args)
{
int[] nums = {7, 8, 9, 0, 0, 1};
Console.WriteLine(zvalue(nums));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
3
时间复杂度: O(N 2 )
辅助空间: O(1)