Find a number K such that exactly K array elements are greater than or equal to K

Given an array a[] of size N, which contains only non-negative elements, the task is to find any integer K for which there are exactly K array elements that are greater than or equal to K. If no such K exists, then print -1.

Examples:

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

 

Approach: The task is to find K such that the array elements are greater than or equal to K. Therefore, K cannot exceed the maximum element present in the array a[n]. Follow the steps below solve the problem:



  1. Traverse the array to find the largest array element, store it in a variable, say m.
  2. Initialize a counter variable, cnt to count the number of array elements greater than or equal to K.
  3. Iterate for possible values of K starting from 0 to m. Iterate over the array for each value and count the number of array elements greater than or equal to that value.
  4. If for any value, exactly K array elements are found to be greater than or equal to that value, print that value.
  5. If no such value is obtained after complete traversal of the array, print -1.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find K for which
// there are exactly K array
// elements greater than or equal to K
int zvalue(vector<int>& 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<int> nums = { 7, 8, 9, 0, 0, 1 };
    cout << zvalue(nums) << endl;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

3








 

Time Complexity: O(N2)
Auxiliary Space: O(1)

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Program Analyst Trainee,Cognizant

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.