#K11076. Find the Next Greater Character in a Sorted Array

    ID: 23388 Type: Default 1000ms 256MiB

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:

  1. The first line contains an integer n representing the number of characters in the array.
  2. The second line contains n space-separated lowercase letters.
  3. 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.

## sample
3
c f j
a
c