Find Smallest Number Greater Than K
EasyPractice library · +10 XPbinary searchsorted arrays
Given a sorted array of distinct integers A and an integer K, find the smallest integer in A that is strictly greater than K. If no such integer exists, report that.
Input
The first line contains two integers N and K, the size of the array and the target value. The second line contains N space-separated distinct integers, the elements of the array A, sorted in ascending order.
Output
Print the smallest integer in A that is strictly greater than K. If no such integer exists, print -1.
Constraints
1 <= N <= 1000 -10^9 <= K <= 10^9 -10^9 <= A[i] <= 10^9 for all i A is sorted in ascending order and contains distinct integers.
Time limit 1 s (native code; slower languages get more)Memory 256 MB13 hidden tests
Example 1
Input
5 3 1 2 4 5 7
Output
4
4 is the smallest element greater than 3.
Example 2
Input
3 10 1 5 9
Output
-1
No element is greater than 10, so output -1.
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 4 5 7
Example 2 input
3 10 1 5 9