#K11076. Find the Next Greater Character in a Sorted Array
Find the Next Greater Character in a Sorted Array
Find the Next Greater Character in a Sorted Array
You are given a sorted array of lowercase letters and a target character. Your task is to find the smallest character in the array that is strictly greater than the given target. If no such character exists, return the first character in the array.
For example:
- For the input array
["c", "f", "j"]
and target'a'
, the output should be'c'
because 'c' is the smallest character greater than 'a'. - For the input array
["c", "f", "j"]
and target'k'
, no character in the array is greater than 'k', so the output is'c'
(the first character).
The function you implement should read from standard input and write to standard output.
inputFormat
The input is read from stdin and has the following format:
- The first line contains an integer n representing the number of characters in the array.
- The second line contains n space-separated lowercase letters.
- The third line contains a single lowercase letter, which is the target character.
outputFormat
Output a single character: the smallest letter in the array that is greater than the target. If no such letter exists, output the first character of the array.
## sample3
c f j
a
c