DEV Community

Cover image for 1552. Magnetic Force Between Two Balls
MD ARIFUL HAQUE
MD ARIFUL HAQUE

Posted on • Edited on

2

1552. Magnetic Force Between Two Balls

1552. Magnetic Force Between Two Balls

Medium

In the universe Earth C-137, Rick discovered a special form of magnetic force between two balls if they are put in his new invented basket. Rick has n empty baskets, the ith basket is at position[i], Morty has m balls and needs to distribute the balls into the baskets such that the minimum magnetic force between any two balls is maximum.

Rick stated that magnetic force between two different balls at positions x and y is |x - y|.

Given the integer array position and the integer m. Return the required force.

Example 1:

q3v1

  • Input: position = [1,2,3,4,7], m = 3
  • Output: 3
  • Explanation: Distributing the 3 balls into baskets 1, 4 and 7 will make the magnetic force between ball pairs [3, 3, 6]. The minimum magnetic force is 3. We cannot achieve a larger minimum magnetic force than 3.

Example 2:

  • Input: position = [5,4,3,2,1,1000000000], m = 2
  • Output: 999999999
  • Explanation: We can use baskets 1 and 1000000000.

Constraints:

  • n == position.length
  • 2 <= n <= 105
  • 1 <= position[i] <= 109
  • All integers in position are distinct.
  • 2 <= m <= position.length

Solution:

class Solution {

    /**
     * @param Integer[] $position
     * @param Integer $m
     * @return Integer
     */
    function maxDistance($position, $m) {
        sort($position);
        $left = 1;
        $right = $position[count($position) - 1];
        while ($left < $right) {
            $mid = ($left + $right + 1) >> 1;
            if ($this->check($position, $mid, $m)) {
                $left = $mid;
            } else {
                $right = $mid - 1;
            }
        }
        return $left;
    }

    public function check($position, $f, $m) {
        $prev = $position[0];
        $cnt = 1;
        for ($i = 1; $i < count($position); ++$i) {
            $curr = $position[$i];
            if ($curr - $prev >= $f) {
                $prev = $curr;
                ++$cnt;
            }
        }
        return $cnt >= $m;
    }
}
Enter fullscreen mode Exit fullscreen mode

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

Billboard image

Monitor more than uptime.

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay