#C14848. Move All Occurrences to End

    ID: 44542 Type: Default 1000ms 256MiB

Move All Occurrences to End

Move All Occurrences to End

You are given a list of integers and an integer N. Your task is to move all occurrences of N to the end of the list while preserving the relative order of the other elements. Formally, if the input list is A and you want to move all occurrences of N, then the output should be a rearranged list B such that

\(B = \{a \in A \mid a \neq N\}\ \Vert\ \{a \in A \mid a = N\}\)

where \(\Vert\) denotes the concatenation operation.

If the element N does not exist in the list, output the original list. Similarly, if the list is empty, output an empty list.

inputFormat

The input is given via stdin and has the following format:

  • The first line contains an integer m representing the number of elements in the list.
  • If m > 0, the second line contains m space-separated integers.
  • The next line contains the integer N, the number that needs to be moved to the end.

outputFormat

The output should be printed to stdout as a single line of space-separated integers representing the rearranged list after moving all occurrences of N to the end.

## sample
7
4 1 2 3 4 5 4
4
1 2 3 5 4 4 4