📌  相关文章
📜  对元素进行计数,以使完全有X个元素的值大于或等于X

📅  最后修改于: 2021-05-06 20:38:38             🧑  作者: Mango

给定一个由N个整数组成的数组arr ,任务是查找满足以下条件的元素数:
如果元素是X,则数组中必须有正好X个元素(不包括数字X ),这些元素大于或等于X

例子:

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

方法:该问题涉及有效搜索每个arr [i]元素的arr [j]的数目(i!= j),该数目大于或等于arr [i]。

  • 以升序对数组进行排序。
  • 对于每个元素arr [i],使用二元搜索获得除arr [i]本身之外所有大于或等于arr [i]的元素的计数。
  • 如果计数等于arr [i],则增加结果。
  • 最后打印结果的值。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long
  
ll int getCount(vector 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 v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
  
    cout << getCount(v, n);
    return 0;
}


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


Python3
# 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


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


输出:
1