What's wrong with my solution


#1

I first sorted array A.
Placed the kth greatest of the array k times in the array G.
As per my understanding, the above logic will give the right answer. But the test is showing error.
I have attached my code:
import java.util.Arrays;

public class Solution {
public int[] solve(int[] A, int[] B) {
Arrays.sort(A);
int[] X = new int[(A.length*(A.length+1))/2];

    int pos=0;
    for(int i=A.length-1; i>=0; i--) {
    	int x = product_of_divisors(A[i]);
    	
    	for(int j=1; j<=i+1; j++ ) {
    		X[pos] = x;
    		pos++;
    	}
    		
    }
   
    int[] ans = new int[B.length];
    for(int i=0; i<B.length; i++) {
    	ans[i] = X[B[i]-1];
  
    }
    
    return ans;
}

private int product_of_divisors(int num) {
	long output = 1;
	for(int i=1; i<=Math.sqrt(num); i++) {
		if (num%i==0) {
			if(i!=Math.sqrt(num)) {
				output = output * (num);
			}else {
				output = output * i;
			}
			
		}
	}

	return (int)(output%1000000007);
}

}