Mike and GCD Issues
Practice
4.4 (38 votes)
Ad Hoc
Approved
Easy
Greatest common divisor
Math
Number theory
Gcd
Problem
59% Success 8364 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Mike claims to be good at math, and a person having a photographic memory. Harvey does trust Mike, but he is not completely sure about these claims said by him. So, for reassurance, he gives the following task to Mike :

Given a 1-indexed array A of size N, the distance between any 2 indices of this array i and j is given by \(|i-j|\). Now, given this information, Mike needs to find for every index i (\( 1 \le i \le N)\), an index j, such that \(1 \le j \le N\), \(i \ne j\), and \(GCD(A[i],A[j]) > 1 \)

If there are multiple such candidates for an index i, you need to find and print the index j, such that the distance between i and j is minimal. If there still exist multiple candidates, print the minimum j satisfying the above constraints.

For each index i of this array, find and print an index j satisfying the conditions above. If, for any index i there does not exist any j, print 1 instead of its answer.

Input Format:

The first line contains a single integer N denoting the size of array A. The next line contains N space separated inetegers, where the \(i^{th}\) integer denotes \(A[i]\).

Output Format :

Print N space separated integers, where the \(i^{th}\) integer denotes an index j, where \(1 \le j \le N\), \(i \ne j\), and GCD\((A[i],A[j])>1\), and the distance between i and j is minimal if there exist multiple candidates satisfying the above constraints. If there still exist multiple candidates satisfying the above constraints, print the minimum j doing so.

Constraints :

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

\( 1 \le A[i] \le 2\times10^5 \)

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
49 votes
Tags:
Basic ProgrammingBit ManipulationBit manipulationMath
Points:20
207 votes
Tags:
ApprovedEasyMathOpenPrimality testPrime FactorizationReady
Points:20
90 votes
Tags:
ApprovedEasyFactorizationMathNumber TheoryOpen