For this Question: You are given an integer array A of size N. For each index i (1 <= i <= N), you need to find an index j, such that gcd(A[i], A[j]) = 1, and abs(i-j) is the minimum possible. If there are two values of j satisfying the condition, report the minimum one. If there is no possible value of j, report -1.

Note: gcd(x, y) represents the the greatest common divisor of integers x and y, while abs(i- j) represents the absolute value of (i-j). Eg: gcd(6, 15) = 3 ; abs(6-15) = 9.

See sample for better understanding.The first line of the input contains a single integer N.
The next line of the input contains N space separated integers, the elements of the array A.

Constraints
1 <= N <= 200000
1 <= A[i] <= 50Output N space separated integers, the value of j corresponding to each index. If there is no possible value of j, report -1 instead.Sample Input
5
1 2 4 3 9

Sample Output
1 1 4 3 3

Explanation
For index 1, gcd(A[1], A[1]) = 1, and abs(1-1) = 0.
For index 2, gcd(A[2], A[1]) = 1, and abs(2-1) = 1. gcd(A[2], A[4) is also equal to 1, but abs(2-4) = 2, which is a greater value.
Similarly for index 3, 4, and 5, gcd(A[3], A[4]) = 1, gcd(A[4], A[3]) = 1, and gcd(A[5], A[3]) = 1.

Sample Input
5
3 3 2 3 3

Sample Output
3 3 2 3 3 

Sample Input
5
3 21 7 7 21

Sample Output
3 -1 1 1 -1 
, I have written this Solution Code: import java.io.*;
import java.util.*;



class Main {
	static int gcd(int a,int b){
		if(a==0) return b;
		return gcd(b%a,a);
	}

	public static void main (String[] args) throws IOException{

		BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

		int n=Integer.parseInt(br.readLine());
		String str[]=br.readLine().split(" ");
		int a[]=new int[n+1];
	int temp[]=new int[51];
	int res[]=new int[n+1];


		for(int i=1;i<=n;i++) a[i]=Integer.parseInt(str[i-1]);

			  for(int i=1;i<=n;i++){
				  	int min=200000;
					  temp[a[i]]=i;
			for(int j=1;j<=50;j++){
               if(temp[j]==0) continue;
			   if(gcd(j,a[i])==1){
				   if(min>Math.abs(i-temp[j])){
					   res[i]=temp[j];
					   min=Math.abs(i-temp[j]);
				   }
			   }
			}
			if(min==200000) res[i]=-1;

			  }
			  temp=new int[51];
			    for(int i=n;i>=1;i--){

		     temp[a[i]]=i;
			for(int j=1;j<=50;j++){
               if(temp[j]==0) continue;
			   if(gcd(j,a[i])==1){
                    if(res[i]==-1){
                        res[i] = temp[j];
                    }
                    else{
						if(Math.abs(i-temp[j]) < Math.abs(i-res[i]))res[i] = temp[j];

                    }

					 }


			  }
				}


		for(int i=1;i<=n;i++){
			System.out.print(res[i]+" ");
		}

	}
}, In this Programming Language: Java, Now tell me if this Code is compilable or not?