Find a number K such that exactly K array elements are greater than or equal to K
Last Updated :
06 Jan, 2022
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:
- Traverse the array to find the largest array element, store it in a variable, say m.
- Initialize a counter variable, cnt to count the number of array elements greater than or equal to K.
- 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.
- If for any value, exactly K array elements are found to be greater than or equal to that value, print that value.
- If no such value is obtained after complete traversal of the array, print -1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int zvalue(vector< int >& nums)
{
int m = *max_element(nums.begin(),
nums.end());
int cnt = 0;
for ( int i = 0; i <= m; i++) {
cnt = 0;
for ( int j = 0; j < nums.size(); j++) {
if (nums[j] >= i)
cnt++;
}
if (cnt == i)
return i;
}
return -1;
}
int main()
{
vector< int > nums = { 7, 8, 9, 0, 0, 1 };
cout << zvalue(nums) << endl;
}
|
Java
import java.io.*;
class GFG{
public static int zvalue( int [] nums)
{
int m = max_element(nums);
int cnt = 0 ;
for ( int i = 0 ; i <= m; i++)
{
cnt = 0 ;
for ( int j = 0 ; j < nums.length; j++)
{
if (nums[j] >= i)
cnt++;
}
if (cnt == i)
return i;
}
return - 1 ;
}
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;
}
public static void main(String args[])
{
int [] nums = { 7 , 8 , 9 , 0 , 0 , 1 };
System.out.println(zvalue(nums));
}
}
|
Python3
def zvalue(nums):
m = max (nums)
cnt = 0
for i in range ( 0 , m + 1 , 1 ):
cnt = 0
for j in range ( 0 , len (nums), 1 ):
if (nums[j] > = i):
cnt + = 1
if (cnt = = i):
return i
return - 1
if __name__ = = '__main__' :
nums = [ 7 , 8 , 9 , 0 , 0 , 1 ]
print (zvalue(nums))
|
C#
using System;
class GFG{
public static int zvalue( int [] nums)
{
int m = max_element(nums);
int cnt = 0;
for ( int i = 0; i <= m; i++)
{
cnt = 0;
for ( int j = 0;
j < nums.Length; j++)
{
if (nums[j] >= i)
cnt++;
}
if (cnt == i)
return i;
}
return -1;
}
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;
}
public static void Main(String []args)
{
int [] nums = {7, 8, 9, 0, 0, 1};
Console.WriteLine(zvalue(nums));
}
}
|
Javascript
<script>
function zvalue(nums)
{
var m = max_element(nums);
var cnt = 0;
for (i = 0; i <= m; i++)
{
cnt = 0;
for (j = 0; j < nums.length; j++)
{
if (nums[j] >= i)
cnt++;
}
if (cnt == i)
return i;
}
return -1;
}
function max_element(nums)
{
var max = nums[0];
for (i = 1; i < nums.length; i++)
max = Math.max(max, nums[i]);
return max;
}
nums = [ 7, 8, 9, 0, 0, 1 ];
document.write(zvalue(nums));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach:
- Find the maximum element in the array and store it.
- Create a count array with size of (maximum element +1) and initialize to 0.
- Traverse over the given array and increment the element count of the corresponding index in the count array
- i.e., if (nums[i] == j) then count[j]=count[j]+1
- Find the suffix sum of the count array
- Traverse through the count array from right to left and update the count value with the sum of all the remaining array elements count to the right of it
- Again traverse through the count array, for any index == count[index], return index
- Else return -1
C++
#include <bits/stdc++.h>
using namespace std;
int solve(vector< int >& nums)
{
int max = *max_element(nums.begin(), nums.end());
int count[max + 1] = { 0 };
for ( int i = 0; i < nums.size(); i++) {
count[nums[i]]++;
}
if (count[max] == max)
return max;
for ( int j = max - 1; j >= 0; j--) {
count[j] += count[j + 1];
if (j == count[j]) {
return j;
}
}
return -1;
}
int main()
{
vector< int > v = { 2, 0, 0 };
cout << solve(v);
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static int solve( int nums[])
{
int max = Arrays.stream(nums).max().getAsInt();
int count[] = new int [max + 1 ];
Arrays.fill(count, 0 );
for ( int i = 0 ; i < nums.length; i++) {
count[nums[i]]++;
}
if (count[max] == max)
return max;
for ( int j = max - 1 ; j >= 0 ; j--) {
count[j] += count[j + 1 ];
if (j == count[j]) {
return j;
}
}
return - 1 ;
}
public static void main (String[] args)
{
int v[] = new int []{ 2 , 0 , 0 };
System.out.println(solve(v));
}
}
|
Python3
def solve(nums):
maxx = max (nums)
count = [ 0 ] * (maxx + 1 )
for i in range ( len (nums)):
count[nums[i]] + = 1
if (count[maxx] = = maxx):
return maxx
for j in range (maxx - 1 , - 1 , - 1 ):
count[j] + = count[j + 1 ]
if (j = = count[j]):
return j
return - 1
v = [ 2 , 0 , 0 ]
print (solve(v))
|
C#
using System;
using System.Linq;
public class GFG{
public static int solve( int [] nums)
{
int max = nums.Max();
int [] count = new int [max + 1];
for ( int i = 0; i < nums.Length; i++) {
count[nums[i]]++;
}
if (count[max] == max)
return max;
for ( int j = max - 1; j >= 0; j--) {
count[j] += count[j + 1];
if (j == count[j]) {
return j;
}
}
return -1;
}
public static void Main ()
{
int [] v = new int []{ 2, 0, 0 };
Console.Write(solve(v));
}
}
|
Javascript
<script>
function solve(nums)
{
var max = Math.max.apply(Math, nums);
var count = new Array(max+1).fill(0);
for ( var i = 0; i < nums.length; i++) {
count[nums[i]]++;
}
if (count[max] == max)
return max;
for ( var j = max - 1; j >= 0; j--) {
count[j] += count[j + 1];
if (j == count[j]) {
return j;
}
}
return -1;
}
var v = [ 2, 0, 0 ];
document.write(solve(v));
</script>
|
Time complexity : O (N) where N is maximum(nums.size(), max element)
Space complexity : O ( max element)
Share your thoughts in the comments
Please Login to comment...