Mohit Raut’s Post

✨ Day 101 of My DSA Challenge – Magnetic Force Between Two Balls 🔷 Problem: 1552. Magnetic Force Between Two Balls 🔷 Goal: Place m balls into sorted basket positions such that the minimum magnetic force (i.e., minimum distance between any two balls) is as large as possible. 🔷 Key Insight: This is another brilliant Binary Search on the Answer problem. Instead of directly computing distances, we binary search for the maximum possible minimum distance that still allows placing all balls. Here’s how the logic works: 1️⃣ Sort all basket positions. 2️⃣ Use binary search on the range of possible distances (0 to max(position) - min(position)). 3️⃣ For each middle distance mid, check feasibility using a greedy approach — place balls while maintaining at least mid distance apart. 4️⃣ If it’s possible → try a larger distance. If not → reduce the distance. 🔷 My Java Approach: Implemented a helper function isPossible() to check if we can place all balls for a given minimum distance. Used Binary Search to maximize that minimum distance. 🔷 Complexity: Time → O(n × log(maxDist)) Space → O(1) This problem beautifully blends sorting, binary search, and greedy placement — showing how abstract concepts like “searching on the answer” translate into real algorithmic reasoning. Every new day strengthens my foundation in Binary Search patterns, sharpening both logic and implementation clarity. 🚀 #Day101 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #BinarySearch #GreedyAlgorithm #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #LearnToCode #CodeEveryday #EngineerMindset #DeveloperJourney #GrowthMindset #KeepLearning #ApnaCollege #AlphaBatch #ShraddhaKhapra

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories