Problem 377
Smallest Letter Greater Than Target
Easy · 10 pts · Array, Binary Search
Problem
You are given a list of lowercase letters sorted in non-decreasing order, and a target letter. Find the smallest letter in the list that is strictly greater than the target. The letters wrap around: if no letter is greater than the target, the answer is the first letter of the list. Binary search finds the first letter greater than the target in O(log N).
Input
First line: N and the target letter. Second line: N letters separated by spaces, in non-decreasing order.
Output
Print the answer letter.
Constraints
2 <= N <= 10000; the list contains at least two different letters
Example input
3 a c f j
Example output
c
Explanation
With target 'a' in c f j, the smallest letter greater than 'a' is 'c'.