DSA Tracker

Visualize

Magnetic Force Between Two Balls

Binary search the ANSWER, then greedily test it

time O(n log n + n log(max - min))space O(1)

Nothing in the basket list is the answer, so there is no cell to search for. Turn it around and ask a yes/no question instead: can the balls be placed with every pair at least d apart? A left-to-right greedy answers that in one pass, and the answers are monotonic — if d works, every smaller gap works, and once d fails every larger gap fails too. That yes/no flip is exactly the sorted-true-then-false shape binary search needs, so we search d itself and keep the largest d that still answers yes.

Details

Animated on: position = 29, 1, 41, 20, 58, 12, 70, 5 and m = 4 balls. Drop at most one ball per basket so that the smallest distance between any two balls is as large as possible.

Problem:Magnetic Force Between Two BallsMedium

Binary search the ANSWER, then greedily test it

step 1 / 10

29
[0]
1
[1]
41
[2]
20
[3]
58
[4]
12
[5]
70
[6]
5
[7]

8 baskets at 29, 1, 41, 20, 58, 12, 70, 5 and 4 balls to drop in. Balls pull on each other harder the closer they are, so we want the placement where the CLOSEST pair of balls is as far apart as possible.

Pseudocode
1FUNCTION maxMinDistance(position, m):
2 position <- SORT(position)
3 low <- 1
4 high <- position[LENGTH(position) - 1] - position[0]
5 WHILE low <= high
6 mid <- (low + high) / 2
7 IF canPlace(position, m, mid)
8 best <- mid
9 low <- mid + 1
10 ELSE
11 high <- mid - 1
12 RETURN best

← / → step · space play · Home restart

Every step in words

Show all 10 steps
  1. 8 baskets at 29, 1, 41, 20, 58, 12, 70, 5 and 4 balls to drop in. Balls pull on each other harder the closer they are, so we want the placement where the CLOSEST pair of balls is as far apart as possible.
  2. Sorted: 1, 5, 12, 20, 29, 41, 58, 70. Distances only make sense along a line, so sorting first means a ball's neighbour is always the previous basket we used, so there is no need to compare against every other ball.
  3. We search the ANSWER, not the array: the smallest gap worth trying is 1, the largest is 70 - 1 = 69. A gap that works means every smaller gap works too, so this range is monotonic and binary search applies.
  4. Try gap 35: walking left to right and grabbing the first basket at least 35 past the last ball runs out after only 2 of the 4 balls (1, 41). Taking the earliest legal basket every time never wastes one, so if that walk cannot fit them nothing can — 35 is too ambitious, and so is every larger gap, high becomes 34.
  5. Try gap 17: walking left to right and grabbing the first basket at least 17 past the last ball fits all 4 balls at 1, 20, 41, 58. So 17 is genuinely reachable — bank it as the best so far and get greedier, low becomes 18.
  6. Try gap 26: walking left to right and grabbing the first basket at least 26 past the last ball runs out after only 3 of the 4 balls (1, 29, 58). Taking the earliest legal basket every time never wastes one, so if that walk cannot fit them nothing can — 26 is too ambitious, and so is every larger gap, high becomes 25.
  7. Try gap 21: walking left to right and grabbing the first basket at least 21 past the last ball runs out after only 3 of the 4 balls (1, 29, 58). Taking the earliest legal basket every time never wastes one, so if that walk cannot fit them nothing can — 21 is too ambitious, and so is every larger gap, high becomes 20.
  8. Try gap 19: walking left to right and grabbing the first basket at least 19 past the last ball fits all 4 balls at 1, 20, 41, 70. So 19 is genuinely reachable — bank it as the best so far and get greedier, low becomes 20.
  9. Try gap 20: walking left to right and grabbing the first basket at least 20 past the last ball runs out after only 3 of the 4 balls (1, 29, 58). Taking the earliest legal basket every time never wastes one, so if that walk cannot fit them nothing can — 20 is too ambitious, and so is every larger gap, high becomes 19.
  10. low 20 has passed high 19, so the range is exhausted. Put the balls at 1, 20, 41, 70 — the closest two are 19 apart, and no placement does better.

Now solve it yourself

Watching is the first half. The second is writing it without the animation.