📌  相关文章
📜  查找排序数组中大于k的元素数

📅  最后修改于: 2021-04-26 07:57:20             🧑  作者: Mango

给定一个排序的整数arr []和一个整数k的数组,任务是查找数组中大于k的元素的数量。注意,数组中可能存在k,也可能不存在k

例子:

方法:想法是执行二进制搜索并找到大于k的元素数量。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the count of elements
// from the array which are greater than k
int countGreater(int arr[], int n, int k)
{
    int l = 0;
    int r = n - 1;
  
    // Stores the index of the left most element
    // from the array which is greater than k
    int leftGreater = n;
  
    // Finds number of elements greater than k
    while (l <= r) {
        int m = l + (r - l) / 2;
  
        // If mid element is greater than
        // k update leftGreater and r
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
  
        // If mid element is less than
        // or equal to k update l
        else
            l = m + 1;
    }
  
    // Return the count of elements greater than k
    return (n - leftGreater);
}
  
// Driver code
int main()
{
    int arr[] = { 3, 3, 4, 7, 7, 7, 11, 13, 13 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    int k = 7;
  
    cout << countGreater(arr, n, k);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
      
// Function to return the count of elements
// from the array which are greater than k
static int countGreater(int arr[], int n, int k)
{
    int l = 0;
    int r = n - 1;
  
    // Stores the index of the left most element
    // from the array which is greater than k
    int leftGreater = n;
  
    // Finds number of elements greater than k
    while (l <= r) {
        int m = l + (r - l) / 2;
  
        // If mid element is greater than
        // k update leftGreater and r
        if (arr[m] > k) {
            leftGreater = m;
            r = m - 1;
        }
  
        // If mid element is less than
        // or equal to k update l
        else
            l = m + 1;
    }
  
    // Return the count of elements greater than k
    return (n - leftGreater);
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 3, 4, 7, 7, 7, 11, 13, 13 };
    int n = arr.length;
  
    int k = 7;
  
    System.out.println(countGreater(arr, n, k));
}
}
  
// This code is contributed by Code_Mech


Python3
# Python 3 implementation of the approach
  
# Function to return the count of elements
# from the array which are greater than k
def countGreater(arr, n, k):
    l = 0
    r = n - 1
  
    # Stores the index of the left most element
    # from the array which is greater than k
    leftGreater = n
  
    # Finds number of elements greater than k
    while (l <= r):
        m = int(l + (r - l) / 2)
  
        # If mid element is greater than
        # k update leftGreater and r
        if (arr[m] > k):
            leftGreater = m
            r = m - 1
  
        # If mid element is less than
        # or equal to k update l
        else:
            l = m + 1
  
    # Return the count of elements 
    # greater than k
    return (n - leftGreater)
  
# Driver code
if __name__ == '__main__':
    arr = [3, 3, 4, 7, 7, 7, 11, 13, 13]
    n = len(arr)
    k = 7
  
    print(countGreater(arr, n, k))
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the count of elements
// from the array which are greater than k
static int countGreater(int[]arr, int n, int k)
{
    int l = 0;
    int r = n - 1;
  
    // Stores the index of the left most element
    // from the array which is greater than k
    int leftGreater = n;
  
    // Finds number of elements greater than k
    while (l <= r) 
    {
        int m = l + (r - l) / 2;
  
        // If mid element is greater than
        // k update leftGreater and r
        if (arr[m] > k) 
        {
            leftGreater = m;
            r = m - 1;
        }
  
        // If mid element is less than
        // or equal to k update l
        else
            l = m + 1;
    }
  
    // Return the count of elements greater than k
    return (n - leftGreater);
}
  
// Driver code
public static void Main()
{
    int[] arr = { 3, 3, 4, 7, 7, 7, 11, 13, 13 };
    int n = arr.Length;
  
    int k = 7;
  
    Console.WriteLine(countGreater(arr, n, k));
}
}
  
// This code is contributed by Code_Mech


PHP
 $k) 
        {
            $leftGreater = $m;
            $r = $m - 1;
        }
  
        // If mid element is less than
        // or equal to k update l
        else
            $l = $m + 1;
    }
  
    // Return the count of elements greater than k
    return ($n - $leftGreater);
}
  
// Driver code
$arr = array(3, 3, 4, 7, 7, 7, 11, 13, 13);
$n = sizeof($arr);
$k = 7;
  
echo countGreater($arr, $n, $k);
  
// This code is contributed
// by Akanksha Rai


输出:
3

时间复杂度: O(log(n)),其中n是数组中元素的数量。