6

Yesterday, I appeared in an interview. I was stuck in one of the question, and I am asking here the same.

There is an array given that shows points on x-axis, there are N points. and M coins also given.

Remember N >= M

You have to maximize the minimum distance between any two points.

Example: Array : 1 3 6 12
         3 coins are given.
         If you put coins on 1, 3, 6 points Then distance between coins are : 2, 3 
         So it gives 2 as answer 
         But we can improve further
         If we put coins at 1, 6, 12 points.
         Then distances are 5, 6
         So correct answer is : 5

Please help me because I am totally stuck in this question.

devsda
  • 4,112
  • 9
  • 50
  • 87

3 Answers3

1

Here's my O(N2) approach to this. First, generate all possible distances;

int dist[1000000][3], ndist = 0;
for(int i = 0; i < n; i ++) {
    for(int j = i + 1; j < n; j ++) {
        dist[ndist][0] = abs(points[i] - points[j]);
        dist[ndist][1] = i; //save the first point
        dist[ndist][2] = j; //save the second point
    }
}

Now sort the distances in decreasing order:

sort(dist, dist + ndist, cmp);

Where cmp is:

bool cmp(int x[], int y[]) {
    return (x[0] > y[0]);
}

Sweep through the array, adding points as long as as you don't have m points chosen:

int chosen = 0;
int ans;
for(int i = 0; i < ndist; i ++) {
    int whatadd = (!visited[dist[i][1]]) + (!visited[dist[i][2]); //how many new points we'll get if we take this one
    if(chosen + whatadd > m) {
        break;
    }
    visited[dist[i][1]] = 1;
    visited[dist[i][2]] = 1;
    chosen += whatadd;
    ans = dist[i][0]; //ans is the last dist[i][0] chosen, since they're sorted in decreasing order
}
if(chosen != m) {
    //you need at most one extra point, choose the optimal available one
    //left as an exercise :)
}

I hope that helped!

Chris
  • 26,544
  • 5
  • 58
  • 71
0

You could use a gready algorithm: you choose the first and last point of the ordered series (as this is the largest possible distance). Then you choose the point closest to the average of them (as any other answer will give a smaller distance in one side), than choose the larger partition. Then repeat it as many times as you need.

WebMonster
  • 2,981
  • 2
  • 22
  • 29
  • WebMonster, I also told the same approach. Firstly sort the array, then put points on stating and end ponts, then put third point near the mid of start and end points. But where we put the forth point – devsda Sep 08 '12 at 07:22
  • Repeat what? where do you put the fourth coin? The fifth? Even with just 4 coins and many equally spaced points this algorithm is clearly wrong because it will put third coin in the middle where there should be no coin. – 6502 Sep 08 '12 at 07:24
  • Use the largest partition, I think this could be similar to a binary seach problem but I haveto think about it :) – WebMonster Sep 08 '12 at 07:28
  • @WebMonster , Can you tell me how you decide where the fourth coin should be placed ? – devsda Sep 09 '12 at 06:23
-1

You have to use Dynamic Programming. Because, you need an optimum answer. Your problem is similar to "Change -making of the coin" problem. Like the problem, you have no of coins and you want to find minimum distance.(Instead of minimum no of coins).

You can read following link: Change Coin problem & Dynamic Programming

Manan Shah
  • 1,042
  • 2
  • 14
  • 26
  • How do you split the problem in subproblems by maintaining the optimality constraint? – 6502 Sep 08 '12 at 07:38
  • Sorry!!! It is my mistake. Dynamic programming is not suitable. I thought in the context of optimal answer. But here problem can not be divided in sub problems. Sorry guys. – Manan Shah Sep 08 '12 at 07:41