🚀 Day 9/100 – Find First and Last Position of Element in Sorted Array | LeetCode (Medium) Continuing my #100DaysOfChallenges journey. Today’s problem strengthened my understanding of Binary Search variations. 🧩 Problem: Given a sorted array, find the starting and ending position of a given target value. If the target is not found, return [-1, -1]. Constraint: Must run in O(log n) time. 🧠 Approach Used: Modified Binary Search (Lower Bound + Upper Bound) Instead of scanning the array linearly, I applied: ✔️ Binary Search to find the first occurrence ✔️ Binary Search to find the last occurrence ✔️ Carefully adjusted search boundaries ✔️ Maintained O(log n) complexity Core idea: When target found → continue searching left (for first position) When target found → continue searching right (for last position) ⏱ Complexity: Time: O(log n) Space: O(1) 🎯 Key Learning: Binary Search has many powerful variations Boundary conditions are critical Interview questions often test edge-case precision This problem strengthened my: 👉 Algorithm optimization thinking 👉 Boundary handling skills 👉 Confidence with search-based problems 91 days to go 🚀 #100DaysOfCode #LeetCode #DSA #BinarySearch #Java #ProblemSolving #CodingJourney #TechGrowth #AIML
Find First and Last Position of Element in Sorted Array | LeetCode
More Relevant Posts
-
Day 61 - LeetCode Journey Solved LeetCode 81: Search in Rotated Sorted Array II (Medium) today — a problem that combines binary search + edge case handling + duplicates. This isn’t just a normal binary search. Here, the array is rotated and may contain duplicates, which makes the decision logic more subtle. 💡 Core Idea: At every step, determine which half is sorted. Then decide whether the target lies in that sorted half. If duplicates block the decision, carefully shrink the search space. ⚡ Key Learning Points: • Applying binary search on a rotated array • Handling duplicate values that break clear ordering • Smart boundary adjustments (low++, high--) • Maintaining efficiency close to O(log n) in most cases The real challenge was not writing the code — It was thinking clearly about all possible scenarios. Problems like this strengthen pattern recognition and deepen understanding of search-based algorithms 💯 ✅ Stronger grip on modified binary search ✅ Better handling of tricky edge cases ✅ Improved logical decision-making Each variation of binary search builds sharper intuition. Still learning. Still improving. Still coding 🚀 #LeetCode #DSA #Java #BinarySearch #Algorithms #ProblemSolving #CodingJourney #Consistency #100DaysOfCode #InterviewPreparation #DeveloperGrowth #KeepCoding
To view or add a comment, sign in
-
-
Day 21 – Binary Tree Maximum Path Sum 🚀 Continuing my DSA journey with Striver’s A2Z Sheet / LeetCode practice. Today’s problem focused on finding the maximum path sum in a binary tree, which is one of the most important advanced tree recursion problems. Approach 1️⃣ Recursively compute left & right subtree sums 2️⃣ Ignore negative paths: left = Math.max(left, 0) right = Math.max(right, 0) 3️⃣ Update global max: max = Math.max(max, root.val + left + right) . 4️⃣ Return: root.val + max(left, right) . Github Code :- https://lnkd.in/gk3GgN-W #LeetCode #DSA #StriverA2Z #CodingJourney #Java #BinaryTree #Recursion #ProblemSolving #CodingPractice #SoftwareEngineering #TechInterviewPrep #Day21 #DeveloperJourney
To view or add a comment, sign in
-
-
🚀 Day 10/100 – First Missing Positive | LeetCode (Hard) Double digits in my #100DaysOfChallenges journey! Today’s challenge was a classic interview problem that tests array manipulation + in-place hashing logic. 🧩 Problem: Given an unsorted integer array, return the smallest missing positive integer. Constraints: Must run in O(n) time Use O(1) extra space 🧠 Approach Used: Cyclic Sort (Index Placement Technique) Instead of using extra space like HashSet, I used an in-place strategy: ✔️ Place each number x at index x - 1 ✔️ Ignore negative numbers and numbers > n ✔️ Swap until every valid number is in its correct position ✔️ Scan the array to find the first index where nums[i] != i + 1 Core Idea: If 1 is at index 0, 2 at index 1, etc., The first mismatch gives the missing positive. 💡 Why This Is Smart: It uses the array itself as a hash structure No extra memory required Maintains linear time complexity ⏱ Complexity: Time: O(n) Space: O(1) 🎯 Key Learning: In-place hashing is powerful Hard problems often combine simple ideas cleverly Edge cases (negatives, duplicates, large values) matter a lot This problem strengthened my: 👉 Index mapping intuition 👉 Optimization mindset 👉 Confidence in solving Hard-level problems 90 days to go 🚀 #100DaysOfCode #LeetCode #DSA #Arrays #CyclicSort #Java #ProblemSolving #TechGrowth #AIML
To view or add a comment, sign in
-
-
🚀 #60DaysOfLeetCode – Day 33 Today’s problem was Valid Parentheses, a classic stack-based problem that tests understanding of balanced expressions. 🔹 Approach Used: Stack Traverse the string character by character. Push all opening brackets (, {, [ onto the stack. For every closing bracket, check: If the stack is empty → invalid. Pop the top element and verify if it matches the corresponding opening bracket. At the end, if the stack is empty → the string is valid. 🔹 Key Learning: This problem reinforces how stacks help manage nested structures and order-sensitive operations. It’s a fundamental concept frequently asked in interviews. 🔹 Complexity: Time: O(n) Space: O(n) A simple yet powerful problem to strengthen core DSA concepts! 💻🔥 #LeetCode #DSA #Java #CodingChallenge #ProblemSolving #LearningInPublic #60DaysOfCode
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟐𝟔 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on generating all possible subsets (Power Set) using two different approaches. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Subsets 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟏 – 𝐁𝐢𝐭 𝐌𝐚𝐧𝐢𝐩𝐮𝐥𝐚𝐭𝐢𝐨𝐧 • Total subsets = 2ⁿ • Used bitmask from 0 to (2ⁿ − 1) • Checked each bit position to decide inclusion • Built subset based on set bits This approach directly maps binary representation to subset selection. 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 𝟐 – 𝐁𝐚𝐜𝐤𝐭𝐫𝐚𝐜𝐤𝐢𝐧𝐠 • Added current subset to result • Recursively explored further elements • Removed last element after recursion (backtrack step) • Generated subsets in a structured recursive tree 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Every element has two choices — include or exclude • Bitmasking provides a clean iterative solution • Backtracking builds intuition about recursion trees • Power set problems are about exploring combinations systematically 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n × 2ⁿ) • Space: O(n) (excluding output) 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Subsets teach a powerful concept — every element gives you a binary decision. 26 days consistent. On to Day 27 🚀 #DSA #Arrays #Backtracking #BitManipulation #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
🚀 Day 46 of #100DaysOfCode Solved 719. Find K-th Smallest Pair Distance on LeetCode 📏 🧠 Key Insight: We need to find the k-th smallest absolute difference among all possible pairs in the array. Brute force (generating all pairs) would be O(n²) → not efficient. Instead, we use Binary Search on Answer + Two Pointers. ⚙️ Approach: 1️⃣ Sort the array 2️⃣ Define search space: 🔹left = 0 (minimum distance) 🔹right = max(nums) - min(nums) 3️⃣ Apply Binary Search: 🔹For a given mid, count how many pairs have distance ≤ mid 4️⃣ Counting pairs efficiently: 🔹Use two pointers 🔹For each i, move j while nums[j] - nums[i] ≤ mid 🔹Add (j - i - 1) to count 5️⃣ If count ≥ k → try smaller distance 6️⃣ Else → increase distance 🎯 Final answer = smallest distance satisfying the condition ⏱️ Time Complexity: O(n log n + n log D) 🔹Sorting + Binary Search with linear check 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #TwoPointers #Algorithms #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
Day 18 #SDE Practicing Binary Search on answer space with time and capacity-based problems. Solved: • Minimum Speed to Arrive on Time (LeetCode 1870) • Capacity To Ship Packages Within D Days (Variation) Key Learning: These problems highlight how to model real-world constraints (time, speed, capacity) into a feasibility check. By searching over a range of possible answers and validating each using binary search, we can efficiently arrive at the optimal solution. #LeetCode #DSA #BinarySearch #Algorithms #Java #SoftwareEngineering
To view or add a comment, sign in
-
🚀 Problem Solved: Pow(x, n) – Fast Exponentiation Today I worked on LeetCode 50 (Medium) — implementing pow(x, n) efficiently. Instead of using the naive O(n) multiplication approach, I applied Binary Exponentiation (Fast Power Algorithm) to reduce the time complexity to O(log n). 🔎 Key Learnings: Handling negative powers correctly Preventing integer overflow (using long for edge cases) Understanding how dividing the exponent by 2 optimizes performance Applying mathematical logic in coding interviews Example: 2¹⁰ → 1024 2⁻² → 0.25 This problem was a great reminder that optimization isn’t optional — it’s essential. Slowly building strong fundamentals, one problem at a time. 💻✨ #LeetCode #ProblemSolving #Java #DataStructures #Algorithms #CodingJourney #InterviewPrep #BinaryExponentiation
To view or add a comment, sign in
-
-
LeetCode Problem #1920 – Build Array from Permutation Today I solved an interesting array problem that helped me understand indexing and array traversal better. 📌 Problem: Given a 0-indexed array `nums`, build a new array `ans` such that: `ans[i] = nums[nums[i]]` 📌 Approach: * Create a new array `ans` with the same length as `nums` * Traverse the array using a `for` loop * For each index `i`, access the value `nums[i]` * Use that value as an index again to get `nums[nums[i]]` * Store the result in `ans[i]` 📌 Key Concepts Practiced: ✔ Array traversal ✔ Nested indexing ✔ Understanding how values can act as indexes 📌 Time Complexity: O(n) – since we traverse the array only once. 📌 Key Takeaway: Sometimes the value inside an array can also represent an index, and understanding this idea helps solve many array-based problems efficiently. 💡 Learning Note This problem improved my understanding of **index-based operations and array manipulation**, which are fundamental concepts for solving many coding interview questions. #LeetCode #CodingPractice #Java #DSA #Arrays #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