I used to think Binary Search only helps find values. Now I'm using it to maximize the minimum. 🚀 Day 91/365 — DSA Challenge Solved: Magnetic Force Between Two Balls Problem: Place m balls in baskets such that the minimum distance between any two balls is maximized. This is the opposite of usual problems. We are maximizing a minimum value. So again, Binary Search on Answer. Pick a distance d: Can we place all balls so that each pair is at least d apart? If yes → try bigger distance If no → reduce distance The greedy part: Always place the next ball in the next valid position. This pattern is also known as: Aggressive Cows Problem ⏱ Time: O(n log range) 📦 Space: O(1) Day 91/365 complete. 💻 274 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
Maximizing Minimum Distance with Binary Search
More Relevant Posts
-
Today's problem was HARD. But the pattern was familiar. Binary Search on Answer... again. 🚀 Day 92/365 — DSA Challenge Solved: Find K-th Smallest Pair Distance Problem: Find the k-th smallest distance among all pairs. Brute force: Generate all pairs → O(n²) → Too slow. Better idea: Binary search the distance. For a distance d: Count how many pairs have distance ≤ d. If count ≥ k → try smaller distance Else → increase distance The interesting part: We count pairs using Sliding Window in O(n). So the final solution becomes: Binary Search + Sliding Window. ⏱ Time: O(n log range) 📦 Space: O(1) Day 92/365 complete. 💻 273 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #SlidingWindow #Algorithms #LearningInPublic
To view or add a comment, sign in
-
-
Yesterday's matrix problem was solved using traversal. Today's matrix problem was solved using Binary Search. Same topic. Different thinking. 🚀 Day 95/365 — DSA Challenge Solved: Search a 2D Matrix Key observation: The matrix is not just row-wise sorted. The first element of each row is greater than the last element of previous row. This means the matrix behaves like a sorted 1D array. So we can apply Binary Search. We convert index → row, col: row = mid / cols col = mid % cols And perform normal binary search. ⏱ Time: O(log(m * n)) 📦 Space: O(1) Day 95/365 complete. 💻 270 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Matrix #LearningInPublic
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 36 Today’s focus: Binary Search for boundaries. Problem solved: • Find First and Last Position of Element in Sorted Array (LeetCode 34) Concepts used: • Binary Search • Lower bound & upper bound • Searching boundaries efficiently Key takeaway: The goal is to find the first and last occurrence of a target in a sorted array. Instead of scanning linearly, we perform two binary searches: • First search → find the leftmost (first) occurrence • Second search → find the rightmost (last) occurrence Key idea: When we find the target: • For left boundary → continue searching in the left half • For right boundary → continue searching in the right half This ensures we capture the full range in O(log n) time. Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 28/160 of my DSA journey Today I solved the Number of Occurrences in a Sorted Array problem. Key Insight: Since the array is sorted, we can use Binary Search to efficiently find the first and last occurrence of the target instead of scanning the entire array. Approach: • Used binary search to find the first occurrence of the target • Used binary search again to find the last occurrence • Calculated count = (last − first + 1) • Returned 0 if target was not found Concepts reinforced: • Binary Search (lower bound & upper bound) • Optimizing from O(n) → O(log n) • Searching in sorted arrays Consistency is key. Improving step by step 🚀 #Day28 #160DaysChallenge #DSA #GeeksforGeeks #LeetCode #CodingJourney #ProblemSolving #Algorithms #Java
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 35 Today’s focus: Binary Search with index patterns. Problem solved: • Single Element in a Sorted Array (LeetCode 540) Concepts used: • Binary Search • Index parity (even/odd pattern) • Search space reduction Key takeaway: The array is sorted and every element appears twice except one. A key observation: Before the single element, pairs start at even indices After the single element, this pattern breaks. Using binary search: • If mid is even and nums[mid] == nums[mid + 1], the single element lies on the right side • Else, it lies on the left side (including mid) By leveraging this pattern, we can find the answer in O(log n) time and O(1) space. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 32 Today’s focus: Binary Search for mathematical validation. Problem solved: • Valid Perfect Square (LeetCode 367) Concepts used: • Binary Search • Search space reduction • Avoiding overflow Key takeaway: The goal is to determine whether a number is a perfect square without using built-in square root functions. Using binary search, we search in the range [1, num]: • Compute mid • Check if mid * mid == num → perfect square • If mid * mid < num, move right • Else move left This reduces the complexity to O(log n). Important detail: To avoid overflow when computing mid * mid, we can use: mid <= num / mid This ensures safe comparison even for large numbers. Continuing to strengthen binary search intuition and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 48 of Daily DSA 🚀 Solved LeetCode 48: Rotate Image ✅ Problem: Given an n x n matrix, rotate the image by 90° clockwise — in-place (without using extra space). Approach: Used a two-step transformation: Transpose the matrix Reverse each row Steps: Traverse upper triangle and swap → matrix[i][j] ↔ matrix[j][i] For each row: Use two pointers (left, right) Swap elements to reverse the row Matrix gets rotated in-place ⏱ Complexity: • Time: O(n²) • Space: O(1) 📊 LeetCode Stats: • Runtime: 0 ms (Beats 100%) ⚡ • Memory: 43.56 MB In-place transformations are powerful — no extra space, just smart manipulation 💡 #DSA #LeetCode #Java #Matrix #Arrays #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 49/75 — Rearrange Array Elements by Sign Today’s problem focused on rearranging an array such that positive and negative numbers alternate, while preserving their original order. Approach: • Use two pointers (even index for positive, odd index for negative) • Traverse the array once • Place elements in correct positions directly Key logic: if (num >= 0) { result[posIdx] = num; posIdx += 2; } else { result[negIdx] = num; negIdx += 2; } Time Complexity: O(n) Space Complexity: O(n) A clean problem that reinforces index placement and pattern-based traversal. 49/75 🚀 #Day49 #DSA #Arrays #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 7/30 — DSA Challenge 🚀 Problem: Search a 2D Matrix Topic: Binary Search Difficulty: Medium Approach: Treated the 2D matrix as a flattened sorted array Applied Binary Search from 0 to (m * n - 1) Converted index → row = mid / m, col = mid % m Mistake / Challenge: Initially thought of searching row by row (O(n + m)) Realized problem expects O(log(m*n)) Fix: Applied Binary Search on the entire matrix Used index mapping to access elements Key Learning: Whenever matrix is sorted like this → think “1D Binary Search” Index mapping trick (row, col) is very powerful Time Taken: 40 minutes Consistency check ✅ See you on Day 8. GitHub Repo: https://lnkd.in/gHW9vKUf #DSA #LeetCode #Java #BinarySearch #Matrix #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 47/60 - DSA Challenge Today’s problem focused on finding a Peak Element using an optimized approach! 💡 🔍 Key Insight: A peak element is one that is greater than its neighbors. Instead of checking every element, I used Binary Search to efficiently locate a peak. ⚡ Approach: Compare the middle element with its next element If the next element is greater, the peak lies on the right side Otherwise, the peak lies on the left side (including the current element) Continue narrowing the search space until the peak is found 📈 Time Complexity: O(log n) 📦 Space Complexity: O(1) 🔥 This problem reinforced how powerful pattern recognition can be—turning a simple idea into an optimized solution using binary search. #Day47 #DSA #BinarySearch #CodingJourney #Java #ProblemSolving #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development