Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find subset of size k such that the minimum distance between values is maximum

Suppose i have an array which contain n integers .
How to find subset of size k such that the minimum distance between all pairs of integers in the subset is maximized , i mean they are at farthest distance .

example : array a[]={1,2,6,7,10} and k=3 ,
subset = {1,6,10} , the minimum distance is 4 between 10 and 6 .
Wrong subsets :
{1,7,10} , minimum distance is 3
{1,2,6} , minimum distance is 1

I came up with a solution :

1) sort array
2) select a[0] , now find ceil(a[0]+ x) = Y in array ....and then ceil(Y+ x) and so on k-1 times , also kth element will be a[n-1]

To find x :
dp[i,j] be the x for selecting j elements from first i elements .
Finally we want dp[n][k] which is x

But i am facing problem in finding x .

dp[i,j] = max( min( dp[k,j-1], dp[i]-A[k] ) )
over k=1 to i-1 , i=2 to n , j=2 to i

dp[i][1] = 0 over i = 1 to n

EDIT : I want to correct the dynamic programming solution , though i know x can be found out by binary searching over x .

UPDATE 2 : I have updated the code , but still not getting correct solution . Please point the error .
code : http://ideone.com/J5vvR9

UPDATE 3 : Thanks @Gassa , @Niklas B. and @Fallen for your answers !.

like image 344
Aseem Goyal Avatar asked Mar 15 '14 14:03

Aseem Goyal


1 Answers

I think there is no need in finding x if time allows to search for possible values of x. Just add the outer loop which will be a binary search on the answer (that is, the minimum distance, let us call it x).

Once x is fixed, you can greedily pick values starting from a[0]. The next selected value will be such a[i] that i is minimal and a[i] - a[0] >= x. The third one will be a[j] such that j is minimal and a[j] - a[i] >= x, and so on. If you are able to pick at least k values in this fashion, the actual answer is at least the current x; if not, the answer is less than x.

The total running time will be O (n log (C)) where C is the total number of possible values in the array. Say, if the integers in the array are from 0 to 1000000, C will be 1000001 and log (C) (rounded up) will be 20. First, you try x = 500000; if it fails, you are left with the range [0; 500000) for the answer; if not, with the range [500000; 1000000], etc.

like image 178
Gassa Avatar answered Oct 19 '22 18:10

Gassa