75 days of solving DSA problems. One thing I've learned so far: Many problems are just variations of classic algorithms. Today was a perfect example. 🚀 Day 75/365 — DSA Challenge Solved: Search in Rotated Sorted Array The Problem Normally, Binary Search works on a sorted array. But what if the array was rotated? 💡 My Approach This problem is solved using a modified Binary Search. At each step: 1️⃣ Find the middle element 2️⃣ Check which side is sorted Left sorted: nums[left] <= nums[mid] Right sorted: nums[mid] <= nums[right] 3️⃣ Decide which half to search based on where the target can exist This keeps the search time at O(log n). Complexity ⏱ Time: O(log n) 📦 Space: O(1) Day 75/365 complete. 💻 290 days to go. Code 👇 https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #BinarySearch #Algorithms #LearningInPublic
Search in Rotated Sorted Array with Binary Search
More Relevant Posts
-
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
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
-
-
#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 27/30 — #30DaysDSAChallenge Today’s topic: Bubble Sort. The concept is simple: Compare adjacent elements and swap them if they are in the wrong order. After each pass through the array, the largest element "bubbles up" to the end. Example: Input: [5,1,4,2,8] After the first pass, the largest element moves to the end of the array. The process repeats until no swaps are needed. ⚙️ Complexity ⏱ Time Complexity: O(n²) 💾 Space Complexity: O(1) Bubble Sort is not efficient for large datasets but, it helps beginners understand how sorting works step by step. 🧠 What I learned today Some algorithms are valuable not because they are efficient, but because they teach the fundamental ideas behind a concept. #DSA #Algorithms #Java #LearningInPublic #ProblemSolving
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 Preparation 💪 Solved a fundamental Binary Search problem, a key concept for efficient searching. Focused on reducing search space by half in each step to achieve optimal performance 🔥 🧠 Problem 🔎 Binary Search Given a sorted array nums and a target value, return its index if found. If the target does not exist, return -1. 👉 The solution must run in O(log n) time complexity. Example Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 ⚡ Key Learning 📌 Binary Search works only on sorted arrays 📌 Each step halves the search space → highly efficient 📌 Time Complexity: O(log n) 📌 Space Complexity: O(1) Improving DSA with strong fundamentals 🚀 #DSA #LeetCode #BinarySearch #Algorithms #Java #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
Day 53 of My DSA Journey Today I solved a problem on searching an element in a rotated sorted array with duplicates. The array is originally sorted but rotated at some pivot. The task is to determine whether a given target value exists in the array. 🔎 My Approach Instead of directly jumping to complex techniques, I used a simple linear search approach: Traverse through the array from start to end. Compare each element with the target value. If a match is found, return true. If the loop finishes without finding the target, return false. 💡 Key Takeaway Sometimes starting with a simple and correct solution is important before optimizing. While more efficient approaches like modified binary search can reduce time complexity, this straightforward method helps clearly understand the problem first. ⏱ Time Complexity: O(n) Every day I’m improving my problem-solving skills in Java and Data Structures & Algorithms step by step. #Day53 #DSA #Java #ProblemSolving #CodingJourney #LeetCode #100DaysOfCode
To view or add a comment, sign in
-
-
🔥 DSA Challenge – Day 127/360 🚀 📌 Topic: Backtracking 🧩 Problem: Subsets II Problem Statement: Given an integer array that may contain duplicates, return all possible subsets such that the solution set does not contain duplicate subsets. 🔍 Example: Input: nums = [1,2,2] Output: [[], [1], [1,2], [1,2,2], [2], [2,2]] 💡 Approach: Backtracking + Sorting 1️⃣ Step 1 – Sort the array to group duplicate elements together 2️⃣ Step 2 – Use recursion to generate all subsets 3️⃣ Step 3 – Skip duplicate elements using condition (i != ind && nums[i] == nums[i-1]) ⏱ Complexity: Time: O(2^n) Space: O(n) (recursion stack + subset storage) 📚 Key Learning: Sorting + smart skipping of duplicates helps avoid repeated subsets in backtracking problems. #DSA #Java #Coding #InterviewPrep #ProblemSolving #TechJourney #360DaysOfCode #LeetCode #Backtracking
To view or add a comment, sign in
-
-
🧠 Day 38 / 100 – DSA Practice Solved Word Search on LeetCode 🔍✅ 🔹 Problem: Given a 2D grid of characters and a word, check if the word exists in the grid by traversing adjacent cells (horizontally or vertically). 🔹 Approach: Used DFS + Backtracking: Start from each cell Explore all 4 directions (up, down, left, right) Mark visited cells to avoid reuse Backtrack if the path doesn’t match 🔍 Key Insight: Backtracking helps explore all possible paths while ensuring each cell is used only once per path 🔹 Complexity: ⏱ Time → O(m × n × 4^L) 📦 Space → O(L) recursion stack 💯 Result: ✔️ All test cases passed ⚡ Runtime: 130 ms (Beats 73%) Great problem to strengthen DFS & Backtracking concepts 🚀 #Day38 #100DaysOfCode #LeetCode #Java #DSA #Backtracking #DFS #Algorithms #CodingJourney
To view or add a comment, sign in
-
-
📘 DSA Journey — Day 36 Today’s focus: Binary Search for boundaries. Problem solved: • Find First and Last Position of Element in Sorted Array (LeetCode 34) Concepts used: • Binary Search • Lower bound & upper bound • Searching boundaries efficiently Key takeaway: The goal is to find the first and last occurrence of a target in a sorted array. Instead of scanning linearly, we perform two binary searches: • First search → find the leftmost (first) occurrence • Second search → find the rightmost (last) occurrence Key idea: When we find the target: • For left boundary → continue searching in the left half • For right boundary → continue searching in the right half This ensures we capture the full range in O(log n) time. Continuing to strengthen binary search patterns and problem-solving consistency. #DSA #Java #LeetCode #CodingJourney
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