![]() |
VOOZH | about |
Given a sorted array of size n, find the number of elements that are less than or equal to a given element and number of elements that are greater than equal to.
Examples :
Input : arr[] = {1, 2, 8, 10, 11, 12, 19} key = 0
Output : 0, 7
Explanation : There are no elements less or equal to 0 and 7 elements greater to 0.Input : arr[] = {1, 5, 8, 12, 12, 12, 19} key = 12
Output : 6, 4
Explanation : There are 6 elements less or equal to 12 and 4 elements greater or equal to 12.
Iterate over the complete array, count elements that are less than or equal to the target, and derive the count of elements greater than the target from the remaining elements.
0 7
As the whole array is sorted we can use binary search to find results.
0 7
Another Approach: Using standard in-built library functions such as lower_bound and upper_bound in C++. or bisect_left and bisect_right() in Python.
0 7
;;