Day 58/100 | #100DaysOfDSA 🔄🔍 Today’s problem: Search in Rotated Sorted Array A twist on Binary Search with a rotated array. Key idea: Even though the array is rotated, one half is always sorted. Approach: • Use binary search • Find mid element • Check which half is sorted (left or right) • Decide if target lies in the sorted half • Narrow the search accordingly Why it works: At every step, we eliminate half of the search space just like standard binary search. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Understanding the structure of the problem helps adapt classic algorithms like binary search. Rotation doesn’t break order — it just shifts it. 🔥 Day 58 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
Rotated Sorted Array Binary Search
More Relevant Posts
-
Day 57/100 | #100DaysOfDSA ⛰️🔍 Today’s problem: Peak Index in a Mountain Array A perfect use-case of Binary Search on a pattern. Key observation: The array increases → reaches a peak → then decreases. Approach: • Use binary search on the index • Compare mid with mid + 1 • If arr[mid] > arr[mid + 1] → we are on the decreasing side → move left • Else → we are on the increasing side → move right This guarantees we always move toward the peak. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Binary search isn’t just for sorted arrays — it works on patterns too. Recognizing these patterns is a game changer. 🔥 Day 57 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
Day 59/100 | #100DaysOfDSA 🔍⚡ Today’s problem: Single Element in a Sorted Array A clever Binary Search problem with a twist. Key observation: In a sorted array where every element appears twice except one, pairs follow a pattern. Before the single element: • First occurrence → even index • Second occurrence → odd index After the single element: • Pattern breaks Approach: • Use binary search • Check mid with its pair using index trick (mid ^ 1) • If nums[mid] == nums[mid ^ 1] → move right • Else → move left This helps pinpoint where the pattern breaks. Time Complexity: O(log n) Space Complexity: O(1) Big takeaway: Bit manipulation + pattern observation can simplify binary search problems significantly. Small tricks → big optimizations. 🔥 Day 59 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #BitManipulation #Arrays #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 2 Today I worked on a DSA problem based on arrays: Check if an array is sorted and rotated 🔍 Approach: Instead of finding the exact rotation point, I focused on identifying a pattern: In a sorted and rotated array, the order should break at most once. So, I checked how many times an element is greater than the next element while traversing the array in a circular manner. ✔️ If the count of such breaks is 0 or 1 → valid ❌ If it’s more than 1 → not a sorted rotated array 🧠 Key Takeaway: This problem taught me how pattern observation can simplify logic and avoid unnecessary complexity. Sometimes the best solution is not the most obvious one! 📈 Staying consistent and improving step by step 💪 #100DaysOfCode #DSA #DataStructures #Algorithms #Java #CodingJourney #ProblemSolving #LeetCode #Consistency
To view or add a comment, sign in
-
-
Day 61/100 | #100DaysOfDSA 🧩🚀 Today’s problem: Median of Two Sorted Arrays A classic hard problem with a clever binary search approach. Problem idea: Find the median of two sorted arrays without fully merging them. Key idea: Use binary search on the smaller array to partition both arrays. Why? • We want left half and right half to be balanced • All elements in left ≤ all elements in right How it works: • Pick a cut in array1 • Derive cut in array2 • Check partition validity using boundary elements If valid → we found the median If not → adjust the partition Time Complexity: O(log(min(m, n))) Space Complexity: O(1) Big takeaway: Binary search isn’t just for searching — it can be used to optimize partitions and positions. This one really builds intuition. 🔥 Day 61 done. #100DaysOfCode #LeetCode #DSA #Algorithms #BinarySearch #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
🚀 #100DaysOfCode | Day 47 🔍 Solved: Find Minimum in Rotated Sorted Array Today I explored another interesting variation of Binary Search. Instead of searching for a target, the goal was to find the minimum element in a rotated sorted array. 💡 Key Insight: By comparing the middle element with the last element, we can determine which half contains the minimum value. Approach: ✔ Used Binary Search to achieve O(log n) time complexity ✔ Compared mid with end to identify the unsorted portion ✔ Narrowed down the search space efficiently What I Learned: This problem helped me understand how binary search can be applied beyond simple searching—especially in rotated and partially sorted arrays. #Java #DSA #LeetCode #BinarySearch #CodingJourney #ProblemSolving #TechSkills
To view or add a comment, sign in
-
-
Day 56/100 | #100DaysOfDSA 🧠⚡ Today’s problem: String Compression A clean in-place array manipulation problem. Core idea: Compress consecutive repeating characters and store the result in the same array. Approach: • Traverse the array using a pointer • Count consecutive occurrences of each character • Write the character to the array • If count > 1 → write its digits one by one • Move forward and repeat Key insight: We don’t need extra space — just carefully manage read & write pointers. Time Complexity: O(n) Space Complexity: O(1) Big takeaway: In-place algorithms require precise pointer control but give optimal space efficiency. Mastering these improves real-world memory optimization skills. 🔥 Day 56 done. #100DaysOfCode #LeetCode #DSA #Algorithms #Strings #TwoPointers #InPlace #Java #CodingJourney #ProblemSolving #InterviewPrep #TechCommunity
To view or add a comment, sign in
-
-
**Day 116 of #365DaysOfLeetCode Challenge** Today’s problem: **Next Greater Element II (LeetCode 503)** A classic **Monotonic Stack** problem with a twist: 👉 The array is **circular** So after the last element, we continue from the first element. 💡 **Core Idea:** For every number, find the **first greater element** while traversing forward. If none exists → return `-1` Example: Input: `[1,2,1]` Output: `[2,-1,2]` Why? * First `1 → 2` * `2 → no greater` * Last `1 → wrap around → 2` 📌 **Efficient Approach: Monotonic Stack** Use stack to store indices whose next greater element is not found yet. Traverse array **twice**: `0 → 2*n - 1` Use: `idx = i % n` This simulates circular behavior. Whenever current number is greater than stack top element: 👉 Pop index 👉 Update answer ⚡ **Time Complexity:** O(n) ⚡ **Space Complexity:** O(n) **What I learned today:** Circular array problems often become simple when you traverse twice using modulo. 👉 `i % n` This trick appears in many advanced array questions. 💭 **Key Takeaway:** When you see: * Next Greater Element * Previous Smaller Element * Nearest Bigger Value #LeetCode #DSA #MonotonicStack #Stack #Arrays #Java #CodingChallenge #ProblemSolving #TechJourney #Consistency
To view or add a comment, sign in
-
-
#Day77 of my second #100DaysOfCode Finally wrapped up binary search on 1D arrays, now moving to binary search on answers. DSA • Solved Sqrt(x) (LeetCode 69) — finding the integer square root of a number without using built-in functions – Brute: iterate until square exceeds target → O(n) – Optimal: binary search on answer space → O(log n) • Key idea: instead of searching an index, we search for the answer itself • Learned how to narrow down the range based on mid² comparison Nice shift in thinking — binary search feels much more powerful now. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 7: Cracking the "Two Pointer" Pattern 🚀 Today, I dived into LeetCode 167 (Two Sum II) to master the Two Pointer technique! While the standard Two Sum problem is often solved with a Hash Map, a Sorted Array gives us a secret advantage. By using two pointers—one at the start and one at the end—we can find the target sum in $O(n)$ time and $O(1)$ space. No extra memory needed! 🧠 💡 Key Takeaway: The magic happens in the movement: Sum < Target? Move the left pointer to grab a larger value. Sum > Target? Move the right pointer to grab a smaller value. Pro Tip: Always watch out for 1-indexed requirements! Adding that +1 to your return indices is the difference between a "Wrong Answer" and "Accepted." ✅ 🛠️ The Logic (Java): Java while (left < right) { int sum = numbers[left] + numbers[right]; if (sum == target) return new int[]{left + 1, right + 1}; else if (sum < target) left++; else right--; } One week down, more patterns to go! Following the roadmap from the "25 DSA Patterns" series. 📈 #DSA #LeetCode #CodingChallenge #Java #TwoPointers #SoftwareEngineering #Consistency
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
-
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