📘 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
Binary Search for First and Last Position in Sorted Array
More Relevant Posts
-
📘 DSA Journey — Day 33 Today’s focus: Binary Search for next greater element. Problem solved: • Find Smallest Letter Greater Than Target (LeetCode 744) Concepts used: • Binary Search • Upper bound concept • Circular handling Key takeaway: The goal is to find the smallest character strictly greater than a given target in a sorted array. This is a classic upper bound problem: We use binary search to find the first element greater than the target. During search: • If letters[mid] > target, store it as a possible answer and move left • Else move right An important edge case: If no character is greater than the target, we return the first element (circular behavior). Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
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 31 Today’s focus: Binary Search for boundaries and square roots. Problems solved: • Sqrt(x) (LeetCode 69) • Search Insert Position (LeetCode 35) Concepts used: • Binary Search • Search space reduction • Boundary conditions Key takeaway: In Sqrt(x), the goal is to find the integer square root. Using binary search, we search in the range [1, x] and check mid * mid against x to narrow down the answer. This avoids linear iteration and achieves O(log n) time. In Search Insert Position, we use binary search to find either: • The exact position of the target, or • The correct index where it should be inserted The key idea is that when the target is not found, the final position of the left pointer gives the correct insertion index. These problems highlight how binary search is not just for finding elements, but also for determining positions and boundaries efficiently. Continuing to strengthen fundamentals and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
#Day79 of second #100DaysOfCode Binary search on answers is starting to click more now. DSA • Solved Minimum Number of Days to Make m Bouquets (LeetCode 1482) • Given bloom days, find the minimum day to make required bouquets – Brute: check each possible day and count valid bouquets → O(n × max(days)) – Optimal: binary search on days → O(n log max(days)) • Key idea: days form a monotonic search space — if a day works, all later days will also work • Focused on counting consecutive flowers correctly while checking feasibility These problems are really helping me understand how to model conditions for binary search. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 28 Today’s focus: Binary Search for minimum in rotated arrays. Problem solved: • Find Minimum in Rotated Sorted Array (LeetCode 153) Concepts used: • Binary Search • Identifying unsorted half • Search space reduction Key takeaway: The goal is to find the minimum element in a rotated sorted array. Using binary search, we compare the mid element with the rightmost element: • If nums[mid] > nums[right] → minimum lies in the right half • Else → minimum lies in the left half (including mid) This works because the rotation creates one unsorted region, and the minimum always lies in that region. By narrowing the search space each time, we achieve O(log n) time complexity. This problem highlights how slight modifications in array structure still allow binary search to work efficiently with the right observations. Continuing to strengthen binary search patterns and consistency in problem solving. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 27 Today’s focus: Binary Search on modified arrays. Problem solved: • Search in Rotated Sorted Array (LeetCode 33) Concepts used: • Binary Search • Identifying sorted halves • Conditional search space reduction Key takeaway: This problem extends binary search to a rotated sorted array, where the array is not fully sorted but divided into two sorted parts. At each step, we: • Find the mid element • Check which half (left or right) is sorted • Decide whether the target lies in the sorted half • Eliminate the other half This allows us to still achieve O(log n) time complexity. Continuing to strengthen fundamentals and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
Day 4 of my DSA consistency journey Solved a classic problem today: Group Anagrams At first glance, it looks simple but if you try brute force (comparing every string with every other), it quickly becomes inefficient. Key idea I used: If two strings are anagrams, their sorted form will be the same. So the approach was: Convert each string to a character array Sort it Use the sorted string as a key in a HashMap Group all original strings under the same key Complexity: Time: O(n * k log k) Learned that this can be further optimized using character frequency instead of sorting Biggest takeaway: It’s not about solving the problem it’s about choosing the right approach early instead of wasting time on brute force. Consistency matters more than intensity. Showing up daily and improving step by step. #DSA #Java #CodingJourney #Consistency #Learning
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 29 Today’s focus: Binary Search on answer space. Problem solved: • Find Peak Element (LeetCode 162) Concepts used: • Binary Search • Observing slope / trend • Search space reduction Key takeaway: The goal is to find a peak element (an element greater than its neighbors). Instead of checking all elements, we use binary search by observing the slope: • If nums[mid] < nums[mid + 1] → we are on an increasing slope, so a peak must exist on the right side • Else → we are on a decreasing slope, so a peak lies on the left side (including mid) By following this logic, we eliminate half of the search space each time and find a peak in O(log n) time. The key insight is: A peak is guaranteed to exist, and the direction of slope helps guide the search. 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
-
-
Leveling up my Array manipulation skills today with the Two-Pointer approach! After mastering basic array traversal, it was time to tackle actual problem-solving patterns. Today, I used the Two-Pointer technique to reverse an array entirely in-place. Instead of creating a second array and wasting memory, you use two indices working together: Pointer 1: Starts at the beginning (left = 0). Pointer 2: Starts at the end (right = n- 1). Action: Swap the elements, then move the pointers toward the middle. Stop: When the pointers meet or cross! The beauty of this pattern? It runs in O(n) time and an incredibly efficient O(1) auxiliary space. Learning how to manipulate data without relying on extra memory is a huge shift in how I think about algorithm design. #DSA #Java #TwoPointerApproach
To view or add a comment, sign in
-
Explore related topics
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