Count Elements Less Than K
EasyPractice library · +10 XPbinary searcharrays
Given a sorted array of N integers and an integer K, find the number of elements in the array that are strictly less than K. The array is sorted in non-decreasing order.
Input
The first line contains two integers N and K, separated by a space. The second line contains N space-separated integers, representing the sorted array.
Output
Print a single integer: the count of elements strictly less than K.
Constraints
1 <= N <= 1000 -10^9 <= A[i] <= 10^9 for all i -10^9 <= K <= 10^9 The array A is sorted in non-decreasing order.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 3 1 2 3 4 5
Output
2
Elements less than 3 are 1 and 2. Count is 2.
Example 2
Input
7 10 2 4 6 8 10 12 14
Output
4
Elements less than 10 are 2, 4, 6, 8. Count is 4.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 3 1 2 3 4 5
Example 2 input
7 10 2 4 6 8 10 12 14