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
Finding Smallest in Rotated Sorted Array with Binary Search
More Relevant Posts
-
#Day74 of my second #100DaysOfCode Binary search variations getting more interesting now. DSA • Solved Find Minimum in Rotated Sorted Array (LeetCode 153) – Brute: linear scan → O(n) – Optimal: binary search with an early check for already sorted part → O(log n) • Key idea: the minimum always lies in the unsorted portion, and if a part is already sorted, the answer can be taken directly • Difference from previous problems: instead of searching for a target, we’re tracking the minimum while narrowing the search space • Edge cases: – already sorted array – single element case – careful updates while narrowing the range This one was more about understanding the pattern than just applying binary search. #DSA #BinarySearch #LeetCode #Algorithms #Java #100DaysOfCode #WomenWhoCode #BuildInPublic #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 43/60 – DSA Challenge Today’s problem was about finding the peak element in a Mountain Array using Binary Search 🔍 🔍 Problem Solved: Given a mountain array (strictly increasing then decreasing), find the index of the peak element. 🧠 Approach I Used: I applied Binary Search on the slope of the array: If the current element is smaller than the next → we are in the increasing part → move right Otherwise → we are in the decreasing part → move left Continue narrowing down until we reach the peak ⚡ Key Insight: Instead of comparing both sides, we only compare with the next element, which simplifies the logic and still guarantees correctness. 📈 Complexity: Time: O(log n) Space: O(1) 🎯 What I Learned: Binary Search can be applied beyond searching—it can solve optimization problems Understanding the pattern of the array (increasing/decreasing) is key Small condition changes can make or break the algorithm Every day is making Binary Search feel more intuitive 🔥 #Day43 #DSAChallenge #BinarySearch #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Imagine standing on a mountain range. Your goal is simple: Find any peak. Not necessarily the highest mountain — just a point that is higher than its neighbors. That's exactly what today's problem was about. 🚀 Day 77/365 — DSA Challenge Solved: Find Peak Element The Problem A peak element is a number that is greater than its neighbors. Given an array, return the index of any peak element. 💡 My Approach This problem can be solved using Binary Search. Key observation: If nums[mid] < nums[mid + 1] It means the peak must be on the right side. Otherwise, the peak lies on the left side (including mid). So we keep shrinking the search space until: start == end That index will be a peak element. Binary search moves toward the increasing slope until it reaches the peak. Complexity ⏱ Time: O(log n) 📦 Space: O(1) Day 77/365 complete. 💻 288 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
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
-
-
Day 82/365 – Shuffle an Array A classic problem that teaches **true randomness** using the Fisher-Yates algorithm. 🔍 **Problem** Design a system to: * Shuffle an array randomly * Reset it back to original 👉 Every permutation must be **equally likely** 💡 **Key Insight** Use **Fisher-Yates Shuffle** (optimal & unbiased) 🧠 **Approach** Iterate from end → start: 1️⃣ Pick random index `j` from `[0, i]` 2️⃣ Swap `i` and `j` This guarantees uniform randomness. ⚡ **Why not random swaps?** 👉 Simple random swapping leads to **biased results** 👉 Fisher-Yates ensures **equal probability** #Day82 #365DaysOfCode #LeetCode #Randomization #Algorithms #DSA #CodingInterview #Java #ProblemSolving
To view or add a comment, sign in
-
-
Today I solved the classic Rotate Array problem — a simple-looking question that teaches a powerful concept: in-place transformation. 💡 Problem: Rotate an array to the right by k steps without using extra space. 🧠 Approach (Optimal): Instead of shifting elements one by one (O(n·k)), I used a smarter trick: ✔️ Reverse the entire array ✔️ Reverse first k elements ✔️ Reverse remaining elements ⚡ This reduces complexity to: ⏱️ Time → O(n) 📦 Space → O(1) 🔥 Key Learning: Sometimes, reversing parts of a data structure can simplify what looks like a complex movement problem. 📌 Problems like this strengthen understanding of: • Two-pointer technique • In-place algorithms • Array manipulation Consistency is key — one problem at a time! 💪 #DataStructures #Algorithms #Java #CodingJourney #LeetCode #ProblemSolving #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 55 of DSA – Median of Two Sorted Arrays Solved a hard binary search problem where we need to find the median of two sorted arrays in O(log(m+n)) time. 💡 Key Insight: Instead of merging both arrays, use binary search on partitions to directly find the correct median. ⚡ Approach: Find partition in the smaller array Adjust partition in the second array accordingly Check if left half and right half are valid Use boundary values to compute median ⏱️ Time Complexity: O(log(min(m, n))) 💾 Space Complexity: O(1) #DSA #LeetCode #Java #Algorithms #BinarySearch #ProblemSolving #CodingJourney
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 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
-
-
The High Cost of the Wrong Initial Choice I have seen complex computation logic for over a million records take hours to run simply because of inefficient, row-by-row processing that is offered by database stored procedures and traditional java/python code. It is slow, impossible to scale, and eventually kills your product's edge in the market. By moving to vectorized logic with tools like NumPy or Polars, you can turn those hours of computation into milliseconds. This is not just about using newer tools, it is about leveraging modern execution like #SIMD (Single Instruction Multiple Data) and #multithreading to gain a massive competitive advantage. If your backend is computation heavy and you're still stuck in legacy loops, you are leaving performance, market edge and money on the table. References: https://lnkd.in/g8j3A5Bn https://lnkd.in/gaPnFUQm https://lnkd.in/gWt9WHnp #DataEngineering #SystemDesign #NumPy #PerformanceOptimization #Scalability #PythonProgramming #TechStrategy #Vectorization #TechToolChoice
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
Keep it up 👍