我有N
整数数组为every index i
其中N<= 10^6
我一定要找到我这样A[j]%A[i]==0 0<j<i
查找最近Divisable在
例
3 4 2 6 7 3
Nearest Neighbor array
-1 -1 1 -1 -1 3
As for last element i.e 3 6%3==0 and index of 6 is 3 so ans is 3
similar for 2 nearest neighbor is 4
蛮力最接近的左邻的数组方法
int[] Neg = new int[n];
Arrays.fill(Neg,-1);
for(int i=1;i<n;i++)
for(int j=i-1;j>=0;j--)
if(A[j]%A[i]==0){
Neg[i]=j;
break;
}
This O(n^2) approach and will fail
我怎么能想出更好的办法可能是O(nlogn)
数组元素的最大可能值是多少? – dreamzor
你的“整数数组”中的值有没有限制?或者整数范围为'-2^31 .. 2^31 - 1' – Aivean
@Aivean值范围在1到10^6 – Sunny