Find the number of elements greater than k in a sorted array Last Updated : 05 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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++ // 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 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 <?php // PHP implementation of the approach // Function to return the count of elements // from the array which are greater than k function 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 JavaScript <script> // Javascript implementation of the approach // Function to return the count of elements // from the array which are greater than k function 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 code var arr = [3, 3, 4, 7, 7, 7, 11, 13, 13]; var n = arr.length; var k = 7; document.write( countGreater(arr, n, k)); </script> Output: 3 Time Complexity: O(log(n)) where n is the number of elements in the array.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find the number of elements greater than k in a sorted array sakshi_srivastava Follow Improve Article Tags : Algorithms Searching Sorting DSA Arrays Binary Search +2 More Practice Tags : AlgorithmsArraysBinary SearchSearchingSorting +1 More Similar Reads Find the Kth smallest element in the sorted generated array Given an array arr[] of N elements and an integer K, the task is to generate an B[] with the following rules: Copy elements arr[1...N], N times to array B[].Copy elements arr[1...N/2], 2*N times to array B[].Copy elements arr[1...N/4], 3*N times to array B[].Similarly, until only no element is left 8 min read Count of numbers in given Array greater than next K elements Given an array arr[] of integers of size N, the task is to count the number of elements whose value is greater than all the K elements to its immediate right. If there are less than K numbers to the right of the ith element, then the value of all of them must be lesser than that of the ith person. E 13 min read Find the Kth occurrence of an element in a sorted Array Given a sorted array arr[] of size N, an integer X, and a positive integer K, the task is to find the index of Kth occurrence of X in the given array. Examples: Input: N = 10, arr[] = [1, 2, 3, 3, 4, 5, 5, 5, 5, 5], X = 5, K = 2Output: Starting index of the array is '0' Second occurrence of 5 is at 15+ min read Find a number K such that exactly K array elements are greater than or equal to K Given an array a[] of size N, which contains only non-negative elements, the task is to find any integer K for which there are exactly K array elements that are greater than or equal to K. If no such K exists, then print -1. Examples: Input: a[] = {7, 8, 9, 0, 0, 1}Output: 3Explanation:Since 3 is le 10 min read Find the number of elements X such that X + K also exists in the array Given an array a[] and an integer k, find the number of elements x in this array such that the sum of x and k is also present in the array. Examples: Input: { 3, 6, 2, 8, 7, 6, 5, 9 } and k = 2Output: 5 Explanation:Elements {3, 6, 7, 6, 5} in this array have x + 2 value that is{5, 8, 9, 8, 7} presen 10 min read Given Array of size n and a number k, find all elements that appear more than n/k times Given an array of size n and an integer k, find all elements in the array that appear more than n/k times. Examples:Input: arr[ ] = [3, 4, 2, 2, 1, 2, 3, 3], k = 4Output: [2, 3]Explanation: Here n/k is 8/4 = 2, therefore 2 appears 3 times in the array that is greater than 2 and 3 appears 3 times in 15+ min read Longest subarray in which all elements are greater than K Given an array of N integers and a number K, the task is to find the length of the longest subarray in which all the elements are greater than K. Examples: Input: a[] = {3, 4, 5, 6, 7, 2, 10, 11}, K = 5 Output: 2 There are two possible longest subarrays of length 2. They are {6, 7} and {10, 11}. Inp 6 min read Number of subarrays with at-least K size and elements not greater than M Given an array of N elements, K is the minimum size of the sub-array, and M is the limit of every element in the sub-array, the task is to count the number of sub-arrays with a minimum size of K and all the elements are lesser than or equal to M. Examples: Input: arr[] = {-5, 0, -10}, K = 1, M = 15O 7 min read Find k smallest elements in an array Given an array arr[] and an integer k, the task is to find k smallest elements in the given array. Elements in the output array can be in any order.Examples:Input: arr[] = [1, 23, 12, 9, 30, 2, 50], k = 3Output: [1, 2, 9]Input: arr[] = [11, 5, 12, 9, 44, 17, 2], k = 2Output: [2, 5]Table of Content[A 15 min read Count elements that are greater than at least K elements on its left and right Given an array arr[] of size n (1 <= n <= 10^5) and a positive integer k, the task is to count all indices i ( 1<= i < n) in the array such that at least k elements to the left of i and at least k elements to the right of i, which are strictly smaller than the value at the ith index (i.e 10 min read Like