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++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; `
`#define ll long long ` ` ` `ll ` `int` `getCount(vector<ll ` `int` `> v, ` `int` `n) `
`{ ` ` ` `// Sorting the vector `
` ` `sort((v).begin(), (v).end()); `
` ` `ll ` `int` `cnt = 0; `
` ` `for` `(ll ` `int` `i = 0; i < n; i++) { `
` ` ` ` `// Count of numbers which `
` ` `// are greater than v[i] `
` ` `ll ` `int` `tmp = v.end() - 1 `
` ` `- upper_bound((v).begin(), (v).end(), v[i] - 1); `
` ` ` ` `if` `(tmp == v[i]) `
` ` `cnt++; `
` ` `} `
` ` `return` `cnt; `
`} ` ` ` `// Driver code ` `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; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of the approach ` `import` `java.util.*; `
` ` `class` `GFG `
`{ ` ` ` `static` `int` `getCount(` `int` `[] v, ` `int` `n) `
` ` `{ `
` ` ` ` `// Sorting the vector `
` ` `Arrays.sort(v); `
` ` `int` `cnt = ` `0` `; `
` ` `for` `(` `int` `i = ` `0` `; i < n; i++) `
` ` `{ `
` ` ` ` `// Count of numbers which `
` ` `// are greater than v[i] `
` ` `int` `tmp = n - ` `1` `- upperBound(v, n, v[i] - ` `1` `); `
` ` `if` `(tmp == v[i]) `
` ` `cnt++; `
` ` `} `
` ` `return` `cnt; `
` ` `} `
` ` ` ` `// Function to implement upper_bound() `
` ` `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; `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `main(String[] args) `
` ` `{ `
` ` `int` `n = ` `4` `; `
` ` `int` `[] v = { ` `1` `, ` `2` `, ` `3` `, ` `4` `}; `
` ` `System.out.println(getCount(v, n)); `
` ` `} `
`} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

`# Python3 implementation of the approach ` `from` `bisect ` `import` `bisect as upper_bound `
` ` `def` `getCount(v, n): `
` ` ` ` `# Sorting the vector `
` ` `v ` `=` `sorted` `(v) `
` ` `cnt ` `=` `0`
` ` `for` `i ` `in` `range` `(n): `
` ` ` ` `# Count of numbers which `
` ` `# are greater than v[i] `
` ` `tmp ` `=` `n ` `-` `1` `-` `upper_bound(v, v[i] ` `-` `1` `) `
` ` ` ` `if` `(tmp ` `=` `=` `v[i]): `
` ` `cnt ` `+` `=` `1`
` ` `return` `cnt `
` ` `# Driver codemain() ` `n ` `=` `4`
`v ` `=` `[] `
`v.append(` `1` `) `
`v.append(` `2` `) `
`v.append(` `3` `) `
`v.append(` `4` `) `
` ` `print` `(getCount(v, n)) `
` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

`// C# implementation of the approach ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `static` `int` `getCount(` `int` `[] v, ` `int` `n) `
` ` `{ `
` ` ` ` `// Sorting the vector `
` ` `Array.Sort(v); `
` ` `int` `cnt = 0; `
` ` `for` `(` `int` `i = 0; i < n; i++) `
` ` `{ `
` ` ` ` `// Count of numbers which `
` ` `// are greater than v[i] `
` ` `int` `tmp = n - 1 - upperBound(v, n, v[i] - 1); `
` ` `if` `(tmp == v[i]) `
` ` `cnt++; `
` ` `} `
` ` `return` `cnt; `
` ` `} `
` ` ` ` `// Function to implement upper_bound() `
` ` `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; `
` ` `} `
` ` ` ` `// Driver Code `
` ` `public` `static` `void` `Main(String[] args) `
` ` `{ `
` ` `int` `n = 4; `
` ` `int` `[] v = { 1, 2, 3, 4 }; `
` ` `Console.WriteLine(getCount(v, n)); `
` ` `} `
`} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

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.

## Recommended Posts:

- Number of ways to choose an integer such that there are exactly K elements greater than it in the given array
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Find a number K such that exactly K array elements are greater than or equal to K
- Smallest subarray of size greater than K with sum greater than a given value
- Minimise N such that sum of count of all factors upto N is greater than or equal to X
- Counting values greater than equal to x after increments
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Count the values greater than X in the modified array
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Find a number K such that Array contains at least K numbers greater than or equal to K
- Queries to count array elements greater than or equal to a given number with updates
- Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Maximum difference between frequency of two elements such that element having greater frequency is also greater
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Sum of all array elements less than X and greater than Y for Q queries
- Count of pairs from arrays A and B such that element in A is greater than element in B at that index

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.