Square will challenge PayPal with its own peer-to-peer cash service
-
Square has launched an under-the-radar payments service that lets customers
transfer cash through email. It's certainly not a new concept, but it's
another...
19 minutes ago
function minimumDistance (arr[],X,Y)
ReplyDelete{
Lung=length(arr[]); //Lunghezza array
PX=Lung; //posizione di X
PY=Lung; //posizione di Y
DXY=Lung; //distanza XY
for(i=0, i abs(PX-PY)) DXY=abs(PX-PY); //Aggiorna la distanza minima
}
}
return DXY;
}
*meglio che calcolare tutte le distanze, O(Lungh)
Just an idea:
ReplyDeleteBuild a map of positions of each unique element in arr[]:
map[a] = list of positions in arr[] where "a" appears.
This require O(n).
Then, generate all the pairs of elements from the lists map[x] and map[y]. Return the pair (a, b) where abs(a - b) - 1 is minimum.
Maybe the pair generation is not the optimum but keep the algo simple to implement :)