Day 33 of DSA 🚀 | Binary Search Today I worked on one of the most fundamental algorithms in problem solving — Binary Search. 🔹 Task: Given a sorted array, find the index of a target element in O(log n) time. 🔹 Key realization: Binary Search is not about guessing. It’s about eliminating half of the search space at every step. 💡 What I learned: Binary Search works only on sorted arrays Using left, right, and mid pointers helps narrow the search efficiently Calculating mid as left + (right - left) / 2 avoids overflow Clear boundary conditions (left <= right) are critical ⏱ Time Complexity: O(log n) 📦 Space Complexity: O(1) This problem reinforced how mastering basics builds the foundation for advanced algorithms. #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #Consistency
Mastering Binary Search in O(log n) Time
More Relevant Posts
-
🔥 Day 27/30 — #30DaysDSAChallenge Today’s topic: Bubble Sort. The concept is simple: Compare adjacent elements and swap them if they are in the wrong order. After each pass through the array, the largest element "bubbles up" to the end. Example: Input: [5,1,4,2,8] After the first pass, the largest element moves to the end of the array. The process repeats until no swaps are needed. ⚙️ Complexity ⏱ Time Complexity: O(n²) 💾 Space Complexity: O(1) Bubble Sort is not efficient for large datasets but, it helps beginners understand how sorting works step by step. 🧠 What I learned today Some algorithms are valuable not because they are efficient, but because they teach the fundamental ideas behind a concept. #DSA #Algorithms #Java #LearningInPublic #ProblemSolving
To view or add a comment, sign in
-
-
Today, I revised Insertion Sort, one of the fundamental sorting algorithms in Data Structures and Algorithms (DSA).Insertion Sort builds the sorted array one element at a time by comparing and inserting elements into their correct position. How it Works? Start from the second element Compare it with previous elements Shift larger elements to the right Insert the element in the correct position. 🔹 Time Complexity: Best Case: O(n) (Already sorted array) Worst Case: O(n²) (Reverse sorted array) 🔹 Space Complexity: O(1) – In-place algorithm #DSA #SortingAlgorithms #InsertionSort #Java #CodingJourney .
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
-
-
🚀 Day 27 of Solving DSA Problems 🧠 Problem: Majority Element (> n/3 times) Today I learned an advanced version of the Boyer–Moore Voting Algorithm. 💡 Key Insight: If an element appears more than ⌊n/3⌋ times, there can be at most 2 such elements. So instead of using a HashMap, we can track only two candidates and cancel out different elements while traversing. After one pass, we verify the candidates’ counts to confirm. ⏱ Complexity: Time → O(n) Space → O(1) 🔥 Lesson: Sometimes you don’t need extra memory — smart logic and observation can replace data structures. Understanding patterns > memorizing solutions 📈 #DSA #Java #ProblemSolving #CodingJourney #LearningInPublic
To view or add a comment, sign in
-
-
Day 7 #SDE Problems involving Binary Search on the answer space and distance-based computations. Solved: • 719. Find K-th Smallest Pair Distance • Sum of Manhattan Distances between all pairs of points Key Learning: Many problems involving pair distances can be optimized by combining sorting + binary search on the answer with efficient counting techniques. Also explored how mathematical observations help compute Manhattan distance sums more efficiently instead of checking every pair. #LeetCode #DSA #BinarySearch #Algorithms #Java #SoftwareEngineering
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 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
-
-
Daily DSA Update – Day 26 Solved: Add Binary (LeetCode) Today’s problem was about adding two binary strings and returning their sum as a binary string. Problem: Given two binary strings a and b, return their sum as a binary string. Approach: I simulated the same process we use while doing binary addition manually. Starting from the end of both strings, I added corresponding digits along with a carry value. After calculating the sum, I appended the result bit and updated the carry. Finally, I reversed the result to get the correct binary order. Key Learning: Problems like this highlight the importance of understanding how operations work internally rather than relying on built-in conversions. What this strengthened: • String traversal from right to left • Handling carry in binary operations • Using StringBuilder for efficient string manipulation • Implementing mathematical logic in code Each daily problem adds a small but meaningful improvement in problem-solving ability. On to the next challenge. #DSA #DataStructures #Algorithms #Java #LeetCode #ProblemSolving #CodingJourney #TechLearning
To view or add a comment, sign in
-
----Continuing my DSA practice, today I solved another hard problem.---- * Problem: Longest Valid Parentheses (32) * Approach: I solved this using a stack-based approach. The idea is to keep track of the indices of parentheses while traversing the string. When encountering "(", its index is pushed onto the stack. When encountering ")", we pop from the stack. If the stack becomes empty, we push the current index as a base reference. Otherwise, we calculate the length of the valid substring using the difference between the current index and the index at the top of the stack. * Key Learning: This problem helped me better understand how stacks can be used to track valid parentheses ranges and compute substring lengths efficiently. These kinds of problems is improving my understanding of stack patterns and string processing . #LeetCode #DSA #Java #ProblemSolving #CodingJourney
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