Shootout in London
Practice
3.3 (8 votes)
Approved
Easy
Hiring
Math
Primality test
Sieve
Problem
54% Success 3875 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There is a major shootout going on in London. Gunmen can be encountered in most of the streets and only a few of them are safe. Sherlock is given the responsibility to ensure the safety of the people by asking them to shift to the nearest safe street.
There are N streets on the road. The \(i^{th}\) street is denoted by the number \(A[i]\). Gunmen do not enter streets that are denoted by a prime number i.e. \(i^{th}\) street is safe, if \(A[i]\) is a prime.

Help Sherlock find the nearest safe street for people trapped in all the N streets i.e. print N integers, where the \(i^{th}\) integer is the index (1-based) of the nearest safe street. The distance between cities i and j is given by \(|i-j|\). For any city j, if there exists no safe city i, such that people of city j can move to city i, print 1 for it.

Note :

-If there are multiple nearest safe streets, print the one with the lower index.

Input:

The first line contains a single integer N denoting the number of cities. The next line contains N integers, where the \(i^{th}\) integer denotes \(A[i]\) .

Output:
Print the answer on a single line.

Constraints:

\( 1 \le N \le 5 \times 10^5 \)

\( 1 \le A[i] \le 10^6 \)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
22 votes
Tags:
ApprovedEasyMathOpenPrimality test
Points:20
16 votes
Tags:
ApprovedBasic ProgrammingEasyMathOpenPrimality testSieve
Points:20
17 votes
Tags:
Ad-HocApprovedBasic ProgrammingEasyImplementationOpen