#K76912. Sort List with Primes First

    ID: 34748 Type: Default 1000ms 256MiB

Sort List with Primes First

Sort List with Primes First

You are given a list of n integers. Your task is to rearrange the list so that all the prime numbers appear at the beginning, followed by all the non‐prime numbers. The relative order of the prime numbers should remain the same as in the original list, and similarly for the non‐prime numbers.

Recall that a number \(n\) is considered prime if it satisfies the following conditions:

  • \(n > 1\)
  • Its only positive divisors are 1 and \(n\) itself.

For example, if the input list is:

[11, 4, 7, 10, 3, 8, 14]

Then the output should be:

[11, 7, 3, 4, 10, 8, 14]

because the prime numbers in the original list are 11, 7, and 3 (in the same order), and the rest are non‐prime numbers.

inputFormat

The first line contains an integer n representing the number of elements in the list. The second line contains n space-separated integers.

Example:

7
11 4 7 10 3 8 14

outputFormat

Output the rearranged list on a single line with the numbers separated by a single space. There should be no extra spaces at the beginning or the end of the line.

Example:

11 7 3 4 10 8 14
## sample
7
11 4 7 10 3 8 14
11 7 3 4 10 8 14

</p>