Solution: Magnetic Force Between Two Balls
Let’s solve the Magnetic Force Between Two Balls problem using the Sort and Search pattern.
We'll cover the following
Statement
In the universe Earth C-137, Rick has discovered a unique type of magnetic force between two balls when placed in his newly invented baskets. He has position[i]
. Morty has m
balls and needs to distribute these balls across the baskets in such a way that the minimum magnetic force between any two balls is as large as possible.
The magnetic force between two balls placed at positions
and is calculated as the absolute difference .
Given an integer array position
and the number of balls m
, return the maximum possible value of the minimum magnetic force between any two balls after they have been placed in the baskets.
Constraints:
position.length
position[i]
All integers in
position
are unique.m
position.length
Solution
The magnetic force between two balls is calculated as the absolute difference, m
balls such that the minimum magnetic force (i.e., the smallest gap between any two consecutive balls) is maximized.
Suppose we aim to place the balls with at least a gap of
If we can place all the balls with at least a gap of
between them, trying smaller gaps is unnecessary, as it will always be possible to place them with a smaller gap. If we can not place all the balls with a gap of
, trying larger gaps is useless since placing them with a larger gap will also be impossible.
As the above dichotomy allows us to discard portions of the search space at each step, it makes binary search an efficient approach. The search range for m
balls. Therefore, dividing the total range by m
balls are spaced as far apart as possible.
Using the intuition above, we implement the algorithm as follows:
The first step is to sort the array
position
.Initialize
force
with0
to store the maximum possible minimum force between balls.Define the binary search range using the variables
low
andhigh
:Initialize
low
with1
.Initialize
high
with(position[position.length - 1] - position[0]) / (m - 1)
.
Use the binary search to find the maximum valid gap. The search continues as long as
low
is less than or equal tohigh
:Calculate the midpoint,
mid
, betweenlow
andhigh
.Call the helper function
CanPlaceBalls(mid, position, m)
to check if it is possible to place allm
balls such that the minimum gap between any two balls is at leastmid
.If
CanPlaceBalls(mid, position, m)
returnstrue
:Update
force = mid
because a gap ofmid
is valid.Increase
low
tomid + 1
to try larger gaps.
If
CanPlaceBalls(mid, position, m)
returnsfalse
:Decrease
high
tomid - 1
because gaps larger thanmid
are not feasible.
Return
force
as the largest minimum magnetic force when the binary search terminates.
The helper function, CanPlaceBalls
, takes the value mid
from the binary search as its parameter (denoted as x
) and is implemented as follows:
Start by placing the first ball in the first position (
position[0]
).Initialize a counter,
balls
, for balls placed.Iterate through the
position
array and check if the distance from the last placed ball to the current position is greater than or equal tox
. If yes:Increment
balls
.Update the last placed ball’s position.
Continue until
m
balls are placed or all positions are exhausted.
Return
true
if exactlym
balls are placed, otherwisefalse
.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.