Count elements such that there are exactly X elements with values greater than or equal to X
Given an array arr of N integers, the task is to find the number of elements that satisfy the following condition:
If the element is X then there has to be exactly X number of elements in the array (excluding the number X) which are greater than or equal to X
Examples:
Input: arr[] = {1, 2, 3, 4}
Output: 1
Only element 2 satisfies the condition as
there are exactly 2 elements which are greater
than or equal to 2 (3, 4) except 2 itself.
Input: arr[] = {5, 5, 5, 5, 5}
Output: 0
Approach: The problem involves efficient searching for each arr[i] element the number of arr[j]’s (i != j) which are greater than or equal to arr[i].
- Sort the array in ascending order.
- For every element arr[i], using binary search get the count of all the elements that are greater than or equal to arr[i] except arr[i] itself.
- If the count is equal to arr[i] then increment the result.
- Print the value of the result in the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll int getCount(vector<ll int > v, int n)
{
sort((v).begin(), (v).end());
ll int cnt = 0;
for (ll int i = 0; i < n; i++) {
ll int tmp = v.end() - 1
- upper_bound((v).begin(), (v).end(), v[i] - 1);
if (tmp == v[i])
cnt++;
}
return cnt;
}
int main()
{
ll int n;
n = 4;
vector<ll int > v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
cout << getCount(v, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int getCount( int [] v, int n)
{
Arrays.sort(v);
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
int tmp = n - 1 - upperBound(v, n, v[i] - 1 );
if (tmp == v[i])
cnt++;
}
return cnt;
}
static int upperBound( int [] array,
int length, int value)
{
int low = 0 ;
int high = length;
while (low < high)
{
final int mid = (low + high) / 2 ;
if (value >= array[mid])
{
low = mid + 1 ;
}
else
{
high = mid;
}
}
return low;
}
public static void main(String[] args)
{
int n = 4 ;
int [] v = { 1 , 2 , 3 , 4 };
System.out.println(getCount(v, n));
}
}
|
Python3
from bisect import bisect as upper_bound
def getCount(v, n):
v = sorted (v)
cnt = 0
for i in range (n):
tmp = n - 1 - upper_bound(v, v[i] - 1 )
if (tmp = = v[i]):
cnt + = 1
return cnt
n = 4
v = []
v.append( 1 )
v.append( 2 )
v.append( 3 )
v.append( 4 )
print (getCount(v, n))
|
C#
using System;
class GFG
{
static int getCount( int [] v, int n)
{
Array.Sort(v);
int cnt = 0;
for ( int i = 0; i < n; i++)
{
int tmp = n - 1 - upperBound(v, n, v[i] - 1);
if (tmp == v[i])
cnt++;
}
return cnt;
}
static int upperBound( int [] array,
int length, int value)
{
int low = 0;
int high = length;
while (low < high)
{
int mid = (low + high) / 2;
if (value >= array[mid])
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low;
}
public static void Main(String[] args)
{
int n = 4;
int [] v = { 1, 2, 3, 4 };
Console.WriteLine(getCount(v, n));
}
}
|
Javascript
<script>
function getCount(v,n)
{
v.sort( function (a,b){ return a-b;});
let cnt = 0;
for (let i = 0; i < n; i++)
{
let tmp = n - 1 - upperBound(v, n, v[i] - 1);
if (tmp == v[i])
cnt++;
}
return cnt;
}
function upperBound(array,length,value)
{
let low = 0;
let high = length;
while (low < high)
{
let mid = Math.floor((low + high) / 2);
if (value >= array[mid])
{
low = mid + 1;
}
else
{
high = mid;
}
}
return low;
}
let n = 4;
let v=[1, 2, 3, 4];
document.write(getCount(v, n));
</script>
|
Complexity Analysis:
- Time Complexity: O(N*logN)
- Auxiliary Space: O(1)
Last Updated :
07 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...