You are given an integer array position[] representing the positions of balls on a number line and an integer m representing the number of balls to place. You must place exactly m balls in the given positions. The magnetic force between two balls is defined as the absolute distance between their positions. Your task is to place the balls such that the minimum magnetic force between any two balls is maximized. Return the maximum possible minimum magnetic force.
Example 1
Input: position = [1,2,3,4,7], m = 3
Output: 3
Explanation: The balls can be placed at positions 1, 4, and 7. The distances are 3 and 3. The minimum magnetic force is 3, which is the maximum possible.
Example 2
Input: position = [5,4,3,2,1,1000000000], m = 2
Output: 999999999
Explanation: The balls can be placed at positions 1 and 1000000000. The distance between them is 999999999, which is the maximum possible.
Example 3
Input: position = [1,3,5,7,9], m = 3
Output: 4
Explanation: The balls can be placed at positions 1, 5, and 9. The distances are 4 and 4. The minimum magnetic force is 4, which is the maximum possible.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 2 3 4 7 3
3
5 4 3 2 1 1000000000 2
999999999
1 2 8 4 9 3
3