🚀 Day 11 – DSA Daily Series Today’s Problem: Find Minimum in Rotated Sorted Array (LeetCode 153) Today I solved an interesting problem that involves finding the minimum element in a rotated sorted array. 🧠 Problem You are given a sorted array that has been rotated several times. The task is to find the minimum element in the array. Example: Input: nums = [3,4,5,1,2] Output: 1 💡 Approach I solved this problem using Binary Search. Key idea: • In a rotated sorted array, the minimum element lies at the rotation point • Compare the middle element with the rightmost element • If nums[mid] > nums[high], the minimum lies in the right half • Otherwise, it lies in the left half including mid By narrowing the search space, we efficiently locate the minimum element. ⏱ Complexity Time Complexity: O(log n) Space Complexity: O(1) 🔎 Key Learning Binary Search can be extended beyond simple searching and used to identify structural properties of arrays like rotation points. Continuing the DSA Daily Series — solving and learning one problem at a time. 🚀 #DSA #LeetCode #Python #Algorithms #BinarySearch #CodingJourney #ProblemSolving
Find Minimum in Rotated Sorted Array with Binary Search
More Relevant Posts
-
🚀 Day 16 – DSA Daily Series Today’s Problem: Two Sum II – Input Array Is Sorted (LeetCode 167) Today’s problem was simple yet really interesting — especially because of how efficiently it can be solved using the two-pointer technique. 🧠 Problem Given a sorted array, find two numbers such that they add up to a target value and return their indices (1-based). Example: Input: [2,7,11,15], target = 9 Output: [1,2] 💡 Approach Instead of using extra space like a hashmap, I used: • Two pointers — one at the start and one at the end • If sum is too large → move right pointer left • If sum is too small → move left pointer right • Stop when target is found Clean and efficient ⚡ ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) 🔎 Key Learning When the array is already sorted, always think of two pointers before anything else — it saves both time and space. Solved it today and it felt really smooth to implement! 💯 Continuing to stay consistent and improve step by step 🚀 #DSA #LeetCode #Python #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
LeetCode Journey – Day 6 Today I solved the Encode and Decode Strings problem At first glance, it looks simple… but the real challenge is handling edge cases where strings may contain special characters like #. My Approach (Optimal Solution): Instead of using delimiters (which can fail), I used length-based encoding. Each string is stored as: length + "#" + string Example: ["neet", "code"] → "4#neet4#code" This ensures: No ambiguity Works with any characters Efficient encoding & decoding Key Learning: Never rely on simple separators when data itself can contain them. Using length prefixing makes the solution robust and interview-ready. Complexity: Time: O(n) Space: O(n) Another step forward in my DSA journey! #LeetCode #DSA #CodingJourney #Python #ProblemSolving #Day6 #TechLearning
To view or add a comment, sign in
-
-
✅ Day 78 of 100 Days LeetCode Challenge Problem: 🔹 #3110 – Score of a String 🔗 https://lnkd.in/g9jgxcdK Learning Journey: 🔹 Today’s problem involved calculating the score of a string based on the absolute differences between ASCII values of adjacent characters. 🔹 I iterated through the string starting from index 1 and computed the difference between the current and previous character using ord(). 🔹 For each pair, I added the absolute difference to a running total. 🔹 This approach efficiently accumulates the score in a single pass. Concepts Used: 🔹 String Traversal 🔹 ASCII Value Conversion (ord()) 🔹 Absolute Difference Calculation Key Insight: 🔹 The problem reduces to comparing adjacent elements, making it a straightforward linear scan. 🔹 Using ord() allows direct access to ASCII values, simplifying the computation. Complexity: 🔹 Time: O(n) 🔹 Space: O(1) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
✅ Day 74 of 100 Days LeetCode Challenge Problem: 🔹 #653 – Two Sum IV: Input is a BST 🔗 https://lnkd.in/g9vDFKMA Learning Journey: 🔹 Today’s problem required checking whether there exist two nodes in a Binary Search Tree whose values sum to k. 🔹 I performed a Depth-First Search (DFS) traversal using a stack to visit all nodes of the tree. 🔹 While traversing, I stored each node’s value in a list. 🔹 Then I used a hash set to track visited values and checked whether the complement (k - value) already exists. 🔹 If the complement is found, it means two numbers sum to k, so the function returns True. Concepts Used: 🔹 Depth-First Search (DFS) 🔹 Stack-based Tree Traversal 🔹 Hash Set for Fast Lookup 🔹 Two Sum Pattern Key Insight: 🔹 The classic Two Sum approach works well after collecting values from the BST. 🔹 Using a set allows O(1) lookup, making it efficient to check complements during iteration. Complexity: 🔹 Time: O(n) 🔹 Space: O(n) #LeetCode #Algorithms #DataStructures #CodingInterview #100DaysOfCode #SoftwareEngineering #Python #ProblemSolving #LearningInPublic #TechCareers
To view or add a comment, sign in
-
-
🚀 DSA Practice – Move All Zeros to the End of an Array Today I tackled a common array manipulation problem: Moving all zeros to the end while maintaining the relative order of non-zero elements. 📌 Problem Statement: Given an array arr[], push all the zeros to the end of the array. The relative order of the non-zero elements must remain the same. 🧠 My Approach: 1️⃣ Initialize a pointer count to keep track of the position where the next non-zero element should be placed. 2️⃣ Traverse the array. Every time a non-zero element is encountered, swap it with the element at the count index and increment count. 3️⃣ By the end of the single traversal, all non-zero elements are at the front, and zeros are naturally pushed to the back. ⚙️ Example: Input: [3, 5, 0, 0, 4] Output: [3, 5, 4, 0, 0] 📊 Complexity: Time Complexity: $O(n)$ (Single pass through the array) Auxiliary Space: $O(1)$ (In-place swap, no extra space used) 💻 Language Used: Python This problem is a classic example of the Two-Pointer technique, which is essential for writing efficient, in-place algorithms. Keeping the momentum going as I prepare for upcoming placements! 💪 #DSA #Python #CodingPractice #ProblemSolving #Arrays #TwoPointers #LearningInPublic
To view or add a comment, sign in
-
-
🚀 Day 25 of Consistency | #75DaysLeetCodeChallenge 🧠 Today’s Problem: Search a 2D Matrix (#74) 💡 Key Learning: This problem teaches how to apply binary search on a 2D matrix by treating it as a flattened sorted array. It’s all about visualizing the structure differently. ⚡ Approach: First apply binary search on rows to find the potential row Check range using first and last element of each row Once row is identified → apply binary search on that row Return true if target found, else false 🧠 Why this works: Matrix follows sorted properties row-wise and across rows Binary search efficiently reduces search space Achieves optimal time complexity → O(log(m × n)) 🔥 Result: ✔️ Runtime: 0 ms (Beats 100%) 📈 This is a classic problem combining matrix + binary search, frequently asked in interviews. A big thanks to Shivam Singh, Nikhil Yadav & Akshat Tiwari for this amazing challenge 🙌 Consistency is compounding. Keep going. 💪 #Day25 #LeetCode #DSA #CodingJourney #100DaysOfCode #Python #BinarySearch #Consistency
To view or add a comment, sign in
-
-
📌 Problem: 1679. Max Number of K-Sum Pairs 💡 Approach: First, sort the array to efficiently apply the two-pointer technique. Initialize two pointers: one at the start (left) and one at the end (right). If the sum equals k, we found a valid pair → increment count and move both pointers If the sum is greater than k, move the right pointer to reduce the sum If the sum is smaller than k, move the left pointer to increase the sum Continue until both pointers meet. ⚙️ Key Insight: Sorting enables efficient pair finding Two-pointer approach avoids checking all pairs (O(n²)) Greedy selection ensures maximum number of operations ⏱️ Time Complexity: O(n log n) (due to sorting) 📦 Space Complexity: O(1) (ignoring sorting space) 📚 What I learned: Two-pointer technique on sorted arrays Optimizing pair problems from brute force to efficient solutions #LeetCode #DSA #Algorithms #Coding #ProblemSolving #Python #TwoPointers #Greedy #InterviewPreparation #CodingJourney
To view or add a comment, sign in
-
📌 Problem: Increasing Triplet Subsequence 💡 Approach: Traverse the array while maintaining two variables: first and second, representing the smallest and second smallest values found so far. If the current number is smaller than first, update first Else if it’s smaller than second, update second If a number is greater than both, we’ve found an increasing triplet This ensures an optimal single-pass solution. ⚙️ Key Insight: Track only two values instead of checking all triplets Greedy + optimization approach reduces complexity significantly ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(1) 📚 What I learned: Greedy strategy for subsequence problems Reducing brute-force O(n³) to optimal linear solution #LeetCode #DSA #Algorithms #Coding #ProblemSolving #Python #Greedy #InterviewPreparation #CodingJourney
To view or add a comment, sign in
-
🚀 Day 22 of Consistency | #75DaysLeetCodeChallenge 🧠 Today’s Problem : Daily Temperatures (#739) 💡 Key Learning: This problem introduces the concept of a monotonic stack, useful for solving “next greater element” type problems efficiently. ⚡ Approach: Use a stack to store indices of temperatures Traverse the array → While current temp > stack top temp → pop & calculate days difference Store result for popped indices Push current index to stack 🧠 Why this works: Monotonic stack keeps decreasing order Helps find next greater element in O(n) Avoids brute force O(n²) 🔥 Result : ✔️ Runtime: 116 ms (Beats 28.07%) 📈 A very important pattern for stack-based optimization problems. A big thanks to Shivam Singh, Nikhil Yadav & Akshat Tiwari for this amazing challenge 🙌 Consistency is compounding. Keep going. 💪 #Day22 #LeetCode #DSA #CodingJourney #100DaysOfCode #Python #Stack #Consistency
To view or add a comment, sign in
-
-
🚀 Optimized Solution: Search in Rotated Sorted Array II Solved the problem "Search in Rotated Sorted Array II" using an optimized Binary Search approach! 🔍 Key Idea: Instead of using linear search, I applied a modified binary search to handle: ✔️ Rotated sorted array ✔️ Presence of duplicates ⚙️ Approach: Used binary search to divide the array Identified sorted halves even with duplicates Carefully handled edge cases where duplicates make it hard to decide the sorted side 📈 Complexity: ⏱️ Time Complexity: O(log n) (Worst case O(n) due to duplicates) 💾 Space Complexity: O(1) ✅ Result: ✔️ All test cases passed ⚡ Efficient and scalable solution This problem really helped me strengthen my understanding of edge cases in binary search and real-world problem-solving 💡 #leetcode #binarysearch #algorithms #coding #python #datastructures #softwaredeveloper #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