Find the number of elements greater than k in a sorted array - GeeksforGeeks

05 Aug, 2021
Given a sorted array arr[] of integers and an integer k, the task is to find the count of elements in the array which are greater than k. Note that k may or may not be present in the array.Examples:  
Input: arr[] = {2, 3, 5, 6, 6, 9}, k = 6 Output: 1Input: arr[] = {1, 1, 2, 5, 5, 7}, k = 8 Output: 0  
 
Approach: The idea is to perform binary search and find the number of elements greater than k. Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the count of elements// from the array which are greater than kint 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 codeint 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 implementation of the approachclass GFG{     // Function to return the count of elements// from the array which are greater than kstatic 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 codepublic 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
# Python 3 implementation of the approach # Function to return the count of elements# from the array which are greater than kdef 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 codeif __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# implementation of the approachusing System; class GFG{     // Function to return the count of elements// from the array which are greater than kstatic 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 codepublic 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// PHP implementation of the approach // Function to return the count of elements// from the array which are greater than kfunction 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 = $l + (int)(($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$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
<script> // Javascript implementation of the approach // Function to return the count of elements// from the array which are greater than kfunction countGreater(arr, n, k){    var l = 0;    var r = n - 1;     // Stores the index of the left most element    // from the array which is greater than k    var leftGreater = n;     // Finds number of elements greater than k    while (l <= r) {        var m = l + parseInt((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 codevar arr = [3, 3, 4, 7, 7, 7, 11, 13, 13];var n = arr.length;var k = 7;document.write( countGreater(arr, n, k)); </script>
3
 
Time Complexity: O(log(n)) where n is the number of elements in the array.Auxiliary Space: O(1)
SURENDRA_GANGWAR
Akanksha_Rai
Code_Mech
noob2000
pankajsharmagfg
Binary Search
Algorithms
Arrays
Searching
Sorting
Arrays
Searching
Sorting
Binary Search
Algorithms
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
DSA Sheet by Love Babbar
Introduction to Algorithms
K means Clustering - Introduction
SCAN (Elevator) Disk Scheduling Algorithms
Arrays in Java
Arrays in C/C++
Stack Data Structure (Introduction and Program)
Program for array rotation
Largest Sum Contiguous Subarray