🚀 Day 22 of #100DaysOfCode Solved 34. Find First and Last Position of Element in Sorted Array on LeetCode 🔍 🧠 Key insight: To find both boundaries of a target in a sorted array, a single binary search isn’t enough. Running two binary searches—one for the leftmost and one for the rightmost occurrence—gives an optimal solution. ⚙️ Approach: 🔹Perform a binary search to find the first occurrence 🔹Perform another binary search to find the last occurrence 🔹Adjust search boundaries after finding the target to expand left/right ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #LearningInPublic #CodingJourney
Binary Search for First and Last Position in Sorted Array
More Relevant Posts
-
🚀 Day 82 of #100DaysOfCode 🔍 Problem Solved: Find Minimum in Rotated Sorted Array Today I worked on a binary search based problem where we need to find the minimum element in a rotated sorted array in O(log n) time. 💡 Key Insight: If the array is already sorted (nums[low] <= nums[high]), then the first element is the minimum. Otherwise: If left half is sorted → minimum must be in right half If right half is unsorted → minimum lies there By carefully adjusting low and high, we narrow down the search space efficiently. 🧠 What I practiced: Modified Binary Search Identifying sorted halves Avoiding unnecessary comparisons #Java #DSA #BinarySearch #LeetCode #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 88- #100DaysOfCode Today I solved Peak Index in a Mountain Array using Binary Search. 🔹 Problem Idea A mountain array increases strictly and then decreases. The goal is to find the index of the peak element. 🔹 Approach Used: Binary Search Instead of checking every element (O(n)), we can use the mountain property: • If arr[mid] > arr[mid+1] → we are in the descending part, so the peak lies on the left side. • Otherwise → we are in the ascending part, so move to the right side. This helps us find the peak in O(log n) time. 📊 Time Complexity: O(log n) 📦 Space Complexity: O(1) #DSA #Java #BinarySearch #LeetCode #CodingJourney #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 74 of #100DaysOfCode Today I solved Search in Rotated Sorted Array from LeetCode using a modified Binary Search approach. 🧠 Problem Insight: Even though the array is rotated, one half is always sorted. By identifying the sorted half, we can decide where to move — left or right. ⚡ Key Learning: Instead of thinking “array is rotated ”, Think → “One side is still sorted ” 💻 Approach: Use Binary Search Check which half is sorted Verify if target lies in that half Reduce search space accordingly ⏱ Complexity: Time: O(log n) Space: O(1) #Day74 #100DaysOfCode #DSA #BinarySearch #Java #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 23/100 – LeetCode Challenge Today’s problem: Partitioning Into Minimum Number of Deci-Binary Numbers 🔹 Key Insight: The minimum number of deci-binary numbers required is equal to the maximum digit present in the string. 🔹 Approach: Traverse through each character in the string Convert it to integer (ch - '0') Track the maximum digit Return the maximum value 🔹 Time Complexity: O(n) 🔹 Space Complexity: O(1) ✨ Simple logic, but powerful observation! Instead of constructing numbers, we just analyze the digits. Consistency > Motivation 💪 #Day23 #100DaysOfCode #LeetCode #Java #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 24 of #100DaysOfCode Solved 167. Two Sum II – Input Array Is Sorted on LeetCode 🔢👉👈 🧠 Key insight: Because the array is already sorted, we can avoid hash maps and use a two-pointer approach to find the target sum efficiently. ⚙️ Approach: 🔹Initialize two pointers at the start and end of the array 🔹Calculate the sum of values at both pointers 🔹If the sum is too large → move the right pointer left 🔹If the sum is too small → move the left pointer right 🔹Stop when the target is found ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #TwoPointers #Java #ProblemSolving #LearningInPublic #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 34 of #100DaysOfCode Solved 852. Peak Index in a Mountain Array on LeetCode ⛰️📈 🧠 Key Insight: A mountain array strictly increases to a peak and then decreases. Instead of scanning the whole array, we can use Binary Search to efficiently find the peak. ⚙️ Approach: 🔹Use binary search with two pointers left and right 🔹Compare arr[mid] with arr[mid + 1] 🔹If arr[mid] > arr[mid + 1] → peak is on the left side (including mid) 🔹Otherwise → peak is on the right side 🔹Continue until left == right, which gives the peak index ⏱️ Time Complexity: O(log n) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
𝗬𝗼𝘂 𝗵𝗮𝘃𝗲 𝗯𝗲𝗲𝗻 𝘂𝘀𝗶𝗻𝗴 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵 𝘄𝗿𝗼𝗻𝗴. It is not just for sorted arrays. The real definition — "Eliminate HALF the search space with each decision." That one shift in thinking unlocks 20+ LeetCode problems. Swipe to see the template, live code, and 7 problems you can now solve with one pattern. 👇 Save this. 🔖 #DSA #BinarySearch #Java #LeetCode #CodingInterview #DebugWithPurpose
To view or add a comment, sign in
-
🚀 Day 28 of #100DaysOfCode Solved 148. Sort List on LeetCode 🔗📊 🧠 Why this problem is interesting: Arrays are easy to sort, but linked lists don’t allow random access. That’s why Merge Sort becomes the perfect choice here. ⚙️ Approach (Merge Sort on Linked List): 🔹Use slow & fast pointers to find the middle 🔹Split the list into two halves 🔹Recursively sort both halves 🔹Merge two sorted linked lists ⏱️ Time Complexity: O(n log n) 📦 Space Complexity: O(log n) (recursive stack) #100DaysOfCode #LeetCode #DSA #LinkedList #MergeSort #Java #ProblemSolving #Consistency #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 60 of DSA consistency Today I practiced a Binary Search Tree (BST) problem: Search in a Binary Search Tree. In this problem, we need to find a node with a given value in a BST and return the subtree rooted at that node. 🔹 Key Idea: A BST has the property: Left subtree values < root Right subtree values > root Using this property, we can efficiently search by moving left or right instead of traversing the entire tree. 📊 Complexity Analysis Time Complexity: O(h) → where h is the height of the BST Space Complexity: O(h) due to recursion stack If the tree is balanced, the complexity becomes O(log n). #DSA #Java #BinarySearchTree #CodingJourney #Consistency #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
LeetCode Problem || Check if Binary String Has at Most One Segment of Ones(1784)🚀 we need to check: The string should have only one continuous block of '1's. After a '0' appears, '1' should never appear again. ✨ Insight: Instead of manually counting segments using loops, we can simply check if "01" exists in the string. 📌 Time Complexity: O(n) Practicing problems like these helps improve pattern recognition and problem-solving efficiency. #LeetCode #DSA #Java #CodingPractice #ProblemSolving
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