🔥 Day 98 of #100DaysOfCode Today’s challenge: LeetCode – Search in Rotated Sorted Array II 🔄🔍 📌 Problem Summary You are given a rotated sorted array that may contain duplicates. Your task is to determine whether a target exists in the array. Example: Input: nums = [2,5,6,0,0,1,2], target = 0 Output: true 🧠 Approach Used: Linear Search In this implementation, we simply iterate through the array and check if the element matches the target. ⚙️ Logic for(int i = 0; i < nums.length; i++){ if(nums[i] == target){ return true; } } return false; 💡 Explanation Traverse the array from start to end If the target is found → return true If the loop finishes → return false ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🚀 Performance Runtime: 0 ms Memory: 44.9 MB 🧠 Learning This problem is a variation of Search in Rotated Sorted Array, but with duplicates allowed. While a Binary Search solution exists, duplicates can break the strict ordering and make the logic more complex. A simple linear scan ensures correctness in all cases. Only 2 days left to complete #100DaysOfCode 🚀 Almost at the finish line! On to Day 99 🔥 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #InterviewPrep
Search in Rotated Sorted Array II with Duplicates
More Relevant Posts
-
🔥 Day 97 of #100DaysOfCode Today’s problem: LeetCode – Search in Rotated Sorted Array 🔄🔍 📌 Problem Summary You are given a rotated sorted array and a target. Your task is to return the index of the target if it exists; otherwise return -1. Example: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 🧠 Approach Used: Linear Search In this solution, we simply iterate through the array and check if the current element equals the target. ⚙️ Logic for(int i = 0; i < nums.length; i++){ if(nums[i] == target){ return i; } } return -1; 💡 Explanation Traverse the array from start to end If the target is found → return the index If the loop finishes → return -1 ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🚀 Performance Runtime: 0 ms Memory: 43.68 MB 🧠 Learning This problem can also be solved using Binary Search in O(log n) by identifying which half of the rotated array is sorted. But for smaller inputs or quick validation, linear scan works correctly and is simple to implement. Only 3 days left to complete the #100DaysOfCode challenge 🚀 Consistency is the real win here! On to Day 98 🔥 #100DaysOfCode #LeetCode #Java #DSA #CodingJourney #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Day 11 – #50DaysLeetCodeChallenge Today I solved the 3Sum problem. 📌 Problem Statement Given an integer array nums, return all the unique triplets [nums[i], nums[j], nums[k]] such that: i ≠ j ≠ k nums[i] + nums[j] + nums[k] = 0 ⚠️ The solution must not contain duplicate triplets. Example: Input: [-1,0,1,2,-1,-4] Output: [[-1,-1,2], [-1,0,1]] 💡 Approach I Used – Sorting + Two Pointers 1️⃣ Sort the array 2️⃣ Fix one element i 3️⃣ Use two pointers: left = i + 1 right = end of array 4️⃣ Check the sum: If sum == 0 → add triplet If sum < 0 → move left++ If sum > 0 → move right-- 5️⃣ Skip duplicates to avoid repeated triplets ⚙️ Key Idea Reduce the problem from 3Sum → 2Sum (using two pointers) Sorting helps in efficient traversal and duplicate handling 🧠 What I learned today ✔️ How sorting simplifies complex problems ✔️ Converting problems into smaller subproblems (3Sum → 2Sum) ✔️ Handling duplicates carefully From simple arrays → advanced pointer techniques 🚀 Consistency is making a difference! #LeetCode #Java #DSA #CodingChallenge #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 35 of my #100DaysOfCode Journey Today, I solved the LeetCode problem: Valid Anagram Problem Insight: Given two strings, check if one is an anagram of the other. Approach: • First, check if the strings have the same length; if not, return false • Convert both strings to character arrays • Sort both arrays • Compare the sorted arrays — if equal, the strings are anagrams Time Complexity: • O(n log n) — due to sorting the arrays Space Complexity: • O(n) — for the character arrays Key Learnings: • Sorting is a simple and effective way to compare character compositions • Edge cases like different lengths should be handled first • Breaking the problem into small steps makes it easy to reason about Takeaway: Sometimes, sorting can reduce a seemingly complex problem into a simple comparison. #DSA #Java #LeetCode #100DaysOfCode #CodingJourney #ProblemSolving #Strings
To view or add a comment, sign in
-
-
🚀 Day 84/100 – 𝐒𝐞𝐚𝐫𝐜𝐡 𝐢𝐧 𝐑𝐨𝐭𝐚𝐭𝐞𝐝 𝐒𝐨𝐫𝐭𝐞𝐝 𝐀𝐫𝐫𝐚𝐲 𝐈𝐈 🔍 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠: Binary search works great on sorted arrays, but duplicates introduce ambiguity — making it harder to decide which half is sorted. 💡 𝐂𝐨𝐫𝐞 𝐈𝐝𝐞𝐚: Use modified binary search Identify the sorted half Handle duplicates by shrinking the search space ⚡ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡: Find 𝐦𝐢𝐝 If 𝐭𝐚𝐫𝐠𝐞𝐭 𝐟𝐨𝐮𝐧𝐝 → 𝐫𝐞𝐭𝐮𝐫𝐧 𝐭𝐫𝐮𝐞 𝐇𝐚𝐧𝐝𝐥𝐞 𝐝𝐮𝐩𝐥𝐢𝐜𝐚𝐭𝐞𝐬 (𝐥𝐨𝐰++) Check which half is sorted Narrow down search accordingly #Day84 #100DaysOfCode #Java #DSA #LeetCode #BinarySearch #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 38 of #100DaysOfCode Solved 154. Find Minimum in Rotated Sorted Array II on LeetCode 🔍 🧠 Key Insight: The array is sorted but rotated, and this version introduces duplicates, which makes the binary search logic slightly trickier. ⚙️ Approach: 🔹Use binary search with left and right pointers 🔹Compare nums[mid] with nums[right]: 🔹If nums[mid] > nums[right] → minimum lies in the right half 🔹If nums[mid] < nums[right] → minimum lies in the left half (including mid) 🔹If equal → safely shrink the search space by decrementing right This handles the ambiguity caused by duplicates. ⏱️ Time Complexity: Average: O(log n) Worst case: O(n) (when many duplicates exist) 📦 Space Complexity: O(1) #100DaysOfCode #LeetCode #DSA #BinarySearch #Arrays #Java #ProblemSolving #InterviewPrep #LearningInPublic
To view or add a comment, sign in
-
-
Day 91 of #365DaysOfLeetCode Challenge Today’s problem: **Arithmetic Slices (LeetCode 413)** An interesting problem that focuses on identifying patterns in subarrays. The goal is to count all contiguous subarrays of length ≥ 3 where the difference between consecutive elements remains constant. 💡 **Key Insight:** Instead of checking every subarray (which would be inefficient), we track the current streak of arithmetic sequences. * If the current 3 elements form an arithmetic sequence → extend the streak * Keep adding the count of valid slices ending at current index * Reset when the pattern breaks 📌 **Approach:** * Use two variables: * `curr` → counts current valid extensions * `total` → accumulates final answer * Traverse from index 2 onward * Compare consecutive differences ⚡ **Time Complexity:** O(n) ⚡ **Space Complexity:** O(1) **What I learned today:** Sometimes, problems that look like they need nested loops can be optimized using pattern tracking and dynamic accumulation. Consistency is key — 91 days down, 274 to go! #LeetCode #DSA #CodingChallenge #Java #ProblemSolving #100DaysOfCode #TechJourney
To view or add a comment, sign in
-
-
🚀 Day 29 of #75daysofLeetCode 2095 – Delete the Middle Node of a Linked List Just solved an interesting linked list problem that perfectly demonstrates the power of the two-pointer technique (slow & fast pointers). 🔍 Problem Insight: Given a linked list, delete its middle node where the middle is defined as ⌊n/2⌋ (0-based indexing). 💡 Key Idea: Instead of calculating the length, we can efficiently find the middle using: 🐢 Slow pointer (1 step) ⚡ Fast pointer (2 steps) When the fast pointer reaches the end, the slow pointer will be at the middle node! 🛠 Approach: ✔ Handle edge case (single node → return null) ✔ Traverse using slow & fast pointers ✔ Keep track of previous node ✔ Remove the middle node in one pass ⏱ Time Complexity: O(n) 💾 Space Complexity: O(1) 🔥 Why this matters? This pattern is widely used in: Finding middle of linked list Detecting cycles Splitting lists Mastering this unlocks many problems! #LeetCode #DSA #LinkedList #Java #CodingInterview #ProblemSolving #TechLearning
To view or add a comment, sign in
-
-
🚀 #60DaysOfLeetCode – Day 32 Today’s problem was about removing all adjacent duplicates in a string. 🔹 Approach Used: Stack To efficiently handle consecutive duplicates, I used a stack-based approach: Traverse the string character by character. If the stack is not empty and the top element matches the current character, pop it (this removes the duplicate pair). Otherwise, push the current character onto the stack. Finally, build the result string from the stack and reverse it to maintain the correct order. 🔹 Key Learning: Using a stack helps simulate the process of removing adjacent duplicates in a single pass, achieving O(n) time complexity and avoiding unnecessary reprocessing. This problem highlights how stacks are powerful for handling sequential patterns and cancellations, especially in string manipulation problems. On to Day 33! 💻🔥 #LeetCode #DSA #Java #CodingChallenge #ProblemSolving #LearningInPublic #60DaysOfCode
To view or add a comment, sign in
-
-
LeetCode 1593 – Split a String Into the Max Number of Unique Substrings An ideal problem to understand Backtracking. The task is simple but tricky: Split a string into substrings such that all substrings are unique, and maximize the number of splits. 🚀 Approach (Backtracking) 1. Start from index i and try every possible substring s[i...j]. 2. If the substring is not already used (checked using a HashSet), we: add it to the set recursively explore the remaining string starting from j + 1 3. Each recursive call increases the current split count. 4. When we reach the end of the string (i >= length), we update the maximum number of unique splits. 5. After recursion, we remove the substring from the set (backtrack) so other possibilities can be explored. # Core Backtracking Pattern Choose → Explore → Undo Choose: Add substring to the set Explore: Recurse for the remaining string Undo: Remove substring to try other partitions Backtracking explores all possible partitions, but the HashSet ensures no substring repeats, giving the maximum valid split. Perfect example of how recursion + state tracking can systematically search the solution space. #LeetCode #Backtracking #Java #DSA #CodingInterview #Recursion
To view or add a comment, sign in
-
-
🚀 LeetCode – Subsets | Backtracking Approach Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets. Return the solution in any order. 💡 Approach Used – Backtracking I used a recursive backtracking strategy where: 1. Start with an empty subset 2. Add the current subset to the result 3. Choose an element and explore further combinations 4. Backtrack by removing the element to try other possibilities This ensures that we explore all possible combinations systematically. Since each element has two choices (include or exclude), the total subsets become 2ⁿ. #LeetCode #DSA #Backtracking #Java #CodingPractice #Algorithms #ProblemSolving #SoftwareEngineering #TechLearning
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