Most people think Binary Search only works on sorted arrays. Today I learned it works on answers too. 🚀 Day 87/365 — DSA Challenge Solved: Capacity To Ship Packages Within D Days Problem: Find the minimum ship capacity to deliver all packages in a given number of days. Brute force? Try every capacity → Too slow. Better idea: Binary Search on the capacity. For each candidate capacity: • Simulate shipping packages • Count days needed • If days <= target → try smaller capacity • Else → increase capacity Key insight: Minimum capacity = max(weights), Maximum capacity = sum(weights) ⏱ Time: O(n log sum(weights)) 📦 Space: O(1) Day 87/365 complete. 💻 278 days to go. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
Binary Search Works on Answers, Not Just Arrays
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
-
-
🚀 Day 43/60 – DSA Challenge Today’s problem was about finding the peak element in a Mountain Array using Binary Search 🔍 🔍 Problem Solved: Given a mountain array (strictly increasing then decreasing), find the index of the peak element. 🧠 Approach I Used: I applied Binary Search on the slope of the array: If the current element is smaller than the next → we are in the increasing part → move right Otherwise → we are in the decreasing part → move left Continue narrowing down until we reach the peak ⚡ Key Insight: Instead of comparing both sides, we only compare with the next element, which simplifies the logic and still guarantees correctness. 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Binary Search can be applied beyond searching—it can solve optimization problems Understanding the pattern of the array (increasing/decreasing) is key Small condition changes can make or break the algorithm Every day is making Binary Search feel more intuitive 🔥 #Day43 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
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
-
-
🚀 Day 42/60 – DSA Challenge Today’s problem was a deeper dive into Binary Search 🔍 — not just finding an element, but locating its first and last occurrence in a sorted array. 🔍 Problem Solved: Given a sorted array, return the starting and ending position of a target element. If not found, return [-1, -1]. 🧠 Approach I Used: Instead of a linear scan, I applied Binary Search twice: First pass → to find the first occurrence (left boundary) Second pass → to find the last occurrence (right boundary) ⚡ Key Insight: For the first occurrence, I kept shrinking the right side whenever I found the target For the last occurrence, I kept expanding to the right when the target matched Carefully handled edge cases to ensure the element actually exists before returning 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Binary Search is not just about finding an element—it’s about controlling boundaries Small changes in conditions (>=, <=) can completely change behavior Validating results is crucial to avoid incorrect answers Consistency + clarity in concepts = progress 🚀 #Day42 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
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
To view or add a comment, sign in
-
-
Day 70/75 — Peak Index in a Mountain Array Binary search on a pattern-based array. Approach: • Since array increases then decreases → peak exists • Use binary search to find peak • If arr[mid] < arr[mid+1] → move right (ascending part) • Else → move left (descending part or peak) Key idea: while (left < right) { int mid = left + (right - left) / 2; if (arr[mid] < arr[mid + 1]) left = mid + 1; else right = mid; } return left; Time Complexity: O(log n) Space Complexity: O(1) Important concept: binary search isn’t just for sorted arrays — it works on patterns. 70/75 ⚡ #Day70 #DSA #BinarySearch #Arrays #Java #LeetCode
To view or add a comment, sign in
-
-
🚀 Day 55 of DSA – Median of Two Sorted Arrays Solved a hard binary search problem where we need to find the median of two sorted arrays in O(log(m+n)) time. 💡 Key Insight: Instead of merging both arrays, use binary search on partitions to directly find the correct median. ⚡ Approach: Find partition in the smaller array Adjust partition in the second array accordingly Check if left half and right half are valid Use boundary values to compute median ⏱️ Time Complexity: O(log(min(m, n))) 💾 Space Complexity: O(1) #DSA #LeetCode #Java #Algorithms #BinarySearch #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 45/60 – DSA Challenge Today’s problem was about searching in a Rotated Sorted Array using Binary Search 🔍 🔍 Problem Solved: Given a rotated sorted array, efficiently find the index of a target element. 🧠 Approach I Used: Instead of directly applying binary search, I broke the problem into two steps: 1️⃣ Find the pivot (rotation point) using binary search 2️⃣ Apply binary search on the correct half of the array Left half → sorted from start to pivot-1 Right half → sorted from pivot to end ⚡ Key Insight: By identifying the pivot, the problem becomes two simple binary searches Careful boundary checks (like target >= nums[0]) ensure we search in the correct half Handling edge cases (like pivot at index 0) is crucial for correctness 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Complex problems can often be simplified by breaking them into smaller parts Binary Search is not just about searching—it’s about understanding structure Boundary conditions can make or break your solution Debugging edge cases today made the concept even clearer 💡 #Day45 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 61/75 — Two Sum II (Input Array Is Sorted) Today’s problem was about finding two numbers in a sorted array that add up to a target. Approach: • Use two-pointer technique (left & right) • If sum > target → move right pointer left • If sum < target → move left pointer right • Stop when sum equals target Key logic: while (i < j) { int sum = numbers[i] + numbers[j]; if (sum == target) return new int[]{i + 1, j + 1}; else if (sum > target) j--; else i++; } Time Complexity: O(n) Space Complexity: O(1) A classic two-pointer problem that reinforces working with sorted arrays efficiently. 61/75 🚀 #Day61 #DSA #TwoPointers #Arrays #Java #Algorithms #LeetCode
To view or add a comment, sign in
-
-
Day 27 of 100: #100DaysDSAChallenge ✅ 💡 Topic: Array Problems (Medium) - Continued 📚 Resource: Striver A2Z DSA Sheet + Playlist 🖥️ Language: Java 📌 What I did today: Solved Set Matrix Zeroes — two approaches, from brute force to optimized. ✅ Problem Solved: • Set Matrix Zeroes 🔑 Key Learnings: → Approach 1 — Brute Force: • For each zero found, mark entire row & column with -1 (skipping existing zeros) • After full scan, replace all -1s with 0 • Time: O(m×n×(m+n)) | Space: O(1) → Approach 2 — Extra Arrays: • Use two marker arrays — one of size m (rows) and one of size n (cols) • Scan matrix → if element is 0, mark its row and col index in the marker arrays • Second pass → if row marker is set, zero out entire row; if col marker is set, zero out entire col • Time: O(m×n) | Space: O(m+n) 💡 The key shift in Approach 2 — instead of modifying the matrix mid-scan (which causes false zeroes), record what needs to change first, then apply it. 🔥 73 more days to go! #Day27 #DSA #100DaysOfDSA #Java #StriverA2Z #Arrays #Matrix #CodingChallenge #DSAJourney
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