#C12875. Replace or Reverse

    ID: 42350 Type: Default 1000ms 256MiB

Replace or Reverse

Replace or Reverse

You are given a list of integers and a target integer n. Your task is to process the list according to the following rules:

  • If the target integer n exists in the list, replace all of its occurrences with the character X.
  • If n is not present in the list, output the list in reverse order.

Additionally, you must ensure that the list contains only integers. If any element in the list is not an integer, the program should raise an error. Similarly, if the target n is not an integer, an error must be raised.

The underlying transformation can be summarized by the following logic in LaTeX:

$$\text{result}(list, n) = \begin{cases}\{\text{'X'} \text{ if } x = n \text{ else } x \text{ for each } x \text{ in list}\}, & \text{if } n \in list;\\ list^{\text{reversed}}, & \text{otherwise.}\end{cases}$$

inputFormat

The input is read from stdin and consists of three parts:

  1. An integer m representing the number of elements in the list.
  2. A line containing m integers separated by spaces. (If m is 0, this line will be empty.)
  3. An integer n on a new line.

outputFormat

The output is written to stdout. It consists of the processed list where:

  • If n was found in the list, each occurrence is replaced with X.
  • If n was not found, the list is output in reverse order.

The elements in the resulting list should be printed on a single line separated by a single space. If the resulting list is empty, output nothing.

## sample
5
1 2 3 2 4
2
1 X 3 X 4