Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Magnetic Force Between Two Balls

XPChallenge Points: 20
levelLevel: Medium

Rick and Morty are working with a strange magnetic reaction between balls. Each basket has a fixed coordinate, and placing a ball creates magnetic force relative to other balls based on the distance between their positions.

Morty must place m balls inside n baskets, but he wants the smallest magnetic force between any two placed balls to be as large as possible.

The force between balls at coordinates x and y is:

|x - y|

Your task is to compute the maximum possible value of this minimum force after placing all m balls optimally.

Example 1:

Input: position = [2, 5, 11, 17, 21], m = 3

Output: 9

Explanation:

Place balls at positions 2, 11, 21 → forces are [9, 10], so minimum = 9. But placing at 2, 12, 22 is not possible. Optimal minimum distance = 10.

Example 2:

Input: position = [4, 8, 15, 16, 23, 42], m = 4

Output: 8

Explanation:

One valid placement: 4, 15, 23, 42 Minimum gap = 7.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result