475. Magnetic Force Between Two Balls (Introduction)

EasyBinary SearchArrayBinary SearchGreedy

You have m balls and n baskets at positions position[]. Place all m balls in baskets to maximise the minimum magnetic force (distance) between any two balls. Return this maximum minimum distance.

Input: Integer array position (basket positions) and integer m (number of balls).

Output: Maximum possible minimum distance between any two balls.

Examples

Example 1
Input: [1,2,3,4,7], 3
Output: 3
Explanation: Place balls at 1,4,7. Min distance = 3.
Example 2
Input: [5,4,3,2,1,1000000000], 2
Output: 999999999
Explanation: Place at 1 and 10^9. Distance = 10^9-1.
Example 3
Input: [1,2,8,10,15], 3
Output: 7
Explanation: Place at 1,8,15. Min distance = 7.

Constraints

Asked by

FlipkartAmazonGoogleMicrosoftMeta
Solve this problem in the editor →