#C6999. Find the First Element Greater Than or Equal to a Target
Find the First Element Greater Than or Equal to a Target
Find the First Element Greater Than or Equal to a Target
You are given a sorted array of n integers and a target integer X. Your task is to find the index of the first element in the array which is greater than or equal to X. If no such element exists, output -1.
This problem can be efficiently solved using binary search. In this algorithm, you narrow down the search space by comparing the middle element with X and updating the boundaries accordingly. The binary search ensures that the solution is achieved in O(log n) time complexity.
Note: The array is 0-indexed, meaning that the first element has index 0.
inputFormat
The first line contains an integer n, representing the number of elements in the sorted array. The second line contains n space-separated integers denoting the array elements. The third line contains an integer X, the target value.
outputFormat
Output a single integer: the index of the first element in the array that is greater than or equal to X. If no such element exists, output -1.## sample
4
1 3 5 7
5
2
</p>