Imagine searching for a number in a list of 1 million sorted numbers. Would you check them one by one? Or eliminate half of the list every step? 🚀 Day 74/365 — DSA Challenge Solved: Binary Search The Problem You're given a sorted array and a target value. Return the index of the target if it exists. Otherwise return -1. 💡 The Idea Behind Binary Search Binary Search works by dividing the search space in half every step. Steps: 1️⃣ Start with the middle element 2️⃣ If it equals the target → return index 3️⃣ If target is larger → search right half 4️⃣ If target is smaller → search left half Repeat until the element is found. ⏱ Time: O(log n) 📦 Space: O(1) Day 74/365 complete. 💻 291 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #Algorithms #BinarySearch #LearningInPublic
Binary Search Algorithm Explanation
More Relevant Posts
-
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
To view or add a comment, sign in
-
-
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 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
-
-
Yesterday's problem was about searching in a rotated array. Today's challenge was slightly different: What if we just needed to find the smallest number in that rotated array? 🚀 Day 76/365 — DSA Challenge Solved: Find Minimum in Rotated Sorted Array The Problem You're given a sorted array that has been rotated at some unknown pivot. 💡 My Approach This problem can be solved using Binary Search. Key observation: In a rotated sorted array, the smallest element is where the rotation happened. Steps: 1️⃣ Find the middle element 2️⃣ Compare it with the right element: nums[mid] > nums[right] 3️⃣ If true → the minimum must be in the right half 4️⃣ Otherwise → the minimum is in the left half (including mid) Complexity ⏱ Time: O(log n) 📦 Space: O(1) Day 76/365 complete. 💻 289 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟓𝟔 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding a peak element using binary search. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Find Peak Element 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 • Used binary search instead of linear scan • Compared the middle element with its next element Logic: • If nums[mid] > nums[mid + 1] → peak lies on the left side (including mid) • Else → peak lies on the right side • Continued until left == right 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Binary search can be applied on patterns, not just sorted arrays • A peak always exists due to problem constraints • Comparing adjacent elements helps determine direction • Reducing the search space is the key idea 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(log n) • Space: O(1) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Binary search is not about sorted arrays — it’s about eliminating half of the search space using logic. 56 days consistent 🚀 On to Day 57. #DSA #Arrays #BinarySearch #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🔥 Day 353 – Daily DSA Challenge! 🔥 Problem: 🔍 Single Element in a Sorted Array Given a sorted array where every element appears twice except one, find that single element in O(log n) time. 💡 Key Insight — Binary Search on Pairs In a perfect paired array: Pairs start at even indices Pattern breaks at the single element We use binary search to detect where this pattern breaks. 🧠 Core Observation Before the single element: pairs → (even, odd) After the single element: pairs shift → (odd, even) So we normalize mid: if mid is odd → make it even ⚡ Algorithm Logic ✅ Find mid ✅ Make mid even ✅ Compare: If nums[mid] == nums[mid+1] → move right Else → move left This narrows down to the single element. ⚙️ Complexity ✅ Time Complexity: O(log n) ✅ Space Complexity: O(1) 💬 Challenge for you 1️⃣ Why do we force mid to be even? 2️⃣ How would you solve this using XOR in O(n)? 3️⃣ What if elements appear thrice except one? #DSA #Day353 #LeetCode #BinarySearch #Arrays #Optimization #Java #ProblemSolving #KeepCoding
To view or add a comment, sign in
-
-
75 days of solving DSA problems. One thing I've learned so far: Many problems are just variations of classic algorithms. Today was a perfect example. 🚀 Day 75/365 — DSA Challenge Solved: Search in Rotated Sorted Array The Problem Normally, Binary Search works on a sorted array. But what if the array was rotated? 💡 My Approach This problem is solved using a modified Binary Search. At each step: 1️⃣ Find the middle element 2️⃣ Check which side is sorted Left sorted: nums[left] <= nums[mid] Right sorted: nums[mid] <= nums[right] 3️⃣ Decide which half to search based on where the target can exist This keeps the search time at O(log n). Complexity ⏱ Time: O(log n) 📦 Space: O(1) Day 75/365 complete. 💻 290 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #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
-
-
Day 76/100 Completed ✅ 🚀 Solved LeetCode – Search a 2D Matrix (Java) ⚡ Implemented an optimized binary search approach by treating the 2D matrix as a flattened sorted array. Converted 1D index into 2D coordinates (row = mid / m, col = mid % m) to efficiently locate the target in O(log(m × n)) time. 🧠 Key Learnings: • Applying binary search on a 2D matrix • Converting 1D index to 2D (row & column mapping) • Reducing time complexity from O(m × n) → O(log(m × n)) • Importance of problem observation (matrix behaves like sorted array) 💯 This problem strengthened my understanding of binary search variations and how to apply it beyond simple 1D arrays. 🔗 Profile: https://lnkd.in/gaJmKdrA #leetcode #datastructures #algorithms #java #matrix #binarysearch #arrays #optimization #problemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
Day 53 of My DSA Journey Today I solved a problem on searching an element in a rotated sorted array with duplicates. The array is originally sorted but rotated at some pivot. The task is to determine whether a given target value exists in the array. 🔎 My Approach Instead of directly jumping to complex techniques, I used a simple linear search approach: Traverse through the array from start to end. Compare each element with the target value. If a match is found, return true. If the loop finishes without finding the target, return false. 💡 Key Takeaway Sometimes starting with a simple and correct solution is important before optimizing. While more efficient approaches like modified binary search can reduce time complexity, this straightforward method helps clearly understand the problem first. ⏱ Time Complexity: O(n) Every day I’m improving my problem-solving skills in Java and Data Structures & Algorithms step by step. #Day53 #DSA #Java #ProblemSolving #CodingJourney #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
I salute your consistency bro 🫡🫡🫡