1552. Magnetic Force Between Two Balls LeetCode Solution

In this guide, you will get 1552. Magnetic Force Between Two Balls LeetCode Solution with the best time and space complexity. The solution to Magnetic Force Between Two Balls problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Magnetic Force Between Two Balls solution in C++
  4. Magnetic Force Between Two Balls solution in Java
  5. Magnetic Force Between Two Balls solution in Python
  6. Additional Resources
1552. Magnetic Force Between Two Balls LeetCode Solution image

Problem Statement of Magnetic Force Between Two Balls

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:

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.

See also  1240. Tiling a Rectangle with the Fewest Squares LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(\texttt{sort} + n\log (\max – \min))
  • Space Complexity: O(\texttt{sort})

1552. Magnetic Force Between Two Balls LeetCode Solution in C++

class Solution {
 public:
  int maxDistance(vector<int>& position, int m) {
    ranges::sort(position);

    int l = 1;
    int r = position.back() - position.front();

    while (l < r) {
      const int mid = r - (r - l) / 2;
      if (numBalls(position, mid) >= m)  // There're too many balls.
        l = mid;
      else  // There're too few balls.
        r = mid - 1;
    }

    return l;
  }

 private:
  int numBalls(const vector<int>& position, int force) {
    int balls = 0;
    int prevPosition = -force;
    for (const int pos : position)
      if (pos - prevPosition >= force) {
        ++balls;
        prevPosition = pos;
      }
    return balls;
  }
};
/* code provided by PROGIEZ */

1552. Magnetic Force Between Two Balls LeetCode Solution in Java

class Solution {
  public int maxDistance(int[] position, int m) {
    Arrays.sort(position);

    int l = 1;
    int r = position[position.length - 1] - position[0];

    while (l < r) {
      final int mid = r - (r - l) / 2;
      if (numBalls(position, mid) >= m) // There're too many balls.
        l = mid;
      else // There're too few balls.
        r = mid - 1;
    }

    return l;
  }

  private int numBalls(int[] position, int force) {
    int balls = 0;
    int prevPosition = -force;
    for (final int pos : position)
      if (pos - prevPosition >= force) {
        balls++;
        prevPosition = pos;
      }
    return balls;
  }
}
// code provided by PROGIEZ

1552. Magnetic Force Between Two Balls LeetCode Solution in Python

class Solution:
  def maxDistance(self, position: list[int], m: int) -> int:
    position.sort()

    l = 1
    r = position[-1] - position[0]

    def numBalls(force: int) -> int:
      balls = 0
      prevPosition = -force
      for pos in position:
        if pos - prevPosition >= force:
          balls += 1
          prevPosition = pos
      return balls

    while l < r:
      mid = r - (r - l) // 2
      if numBalls(mid) >= m:
        l = mid
      else:
        r = mid - 1

    return l
# code by PROGIEZ

Additional Resources

See also  646. Maximum Length of Pair Chain LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.