#K11356. Transform List to Nearest Prime
Transform List to Nearest Prime
Transform List to Nearest Prime
You are given a list of integers. Your task is to transform the list by replacing each non-prime number with the nearest smaller prime number. If the number itself is a prime, it remains unchanged. In this problem, a number \( n \) is considered prime if \( n > 1 \) and it has no divisors other than 1 and itself.
The function must behave as follows:
- If the number is prime, keep it.
- If the number is not prime, find the largest prime number that is less than the given number. Note that for any number \( n \leq 2 \), the result should be 2.
For example, transforming the list [10, 17, 18, 19, 20, 23]
results in [7, 17, 17, 19, 19, 23]
since:
- 10 is not prime and its nearest smaller prime is 7.
- 17, 19, and 23 are prime and remain unchanged.
- 18 is not prime and its nearest smaller prime is 17.
- 20 is not prime and its nearest smaller prime is 19.
inputFormat
The input is read from stdin
and consists of two lines:
- The first line contains a single integer \( n \) representing the number of elements in the list.
- The second line contains \( n \) space-separated integers representing the list.
outputFormat
Output a single line to stdout
containing the transformed list. The numbers in the output must be space-separated.
6
10 17 18 19 20 23
7 17 17 19 19 23