Taking the Two-Pointer technique to LeetCode! Today's challenge: Remove Duplicates from a Sorted Array. After practicing the Two-Pointer pattern for reversing arrays and swapping 0s and 1s, I applied it to a classic LeetCode problem. The challenge? Remove duplicates from an array in-place with O(1) extra memory. Because the array is already sorted, all duplicates are grouped together. • The Slow Pointer (The Writer): Keeps track of where the next unique element should be placed. • The Fast Pointer (The Reader): Scans ahead through the array to find new, unique numbers. • The Logic: If the Reader finds a duplicate, it just skips it. But the moment the Reader finds a new number, the Writer records it at the front of the array and steps forward. #DSA #Java #LeetCode #RemoveDuplicate
Remove Duplicates from Sorted Array in O(1) Space
More Relevant Posts
-
The Two-Pointer streak continues! Today’s LeetCode Problem: Is Subsequence. After using multiple pointers to sort arrays and move zeroes, I applied the exact same pattern to string manipulation today. The challenge was figuring out if a shorter string (s) is a valid subsequence of a longer string (t) without disturbing the relative order of the characters. Instead of generating all possible subsequences (which would be a massive O(2ⁿ) performance drain), the Two-Pointer approach solves this beautifully in a single pass. Knocking this out in O(n) time and O(1) space is another great reminder of how incredibly versatile this algorithmic pattern is for both arrays and strings. #DSA #Java #LeetCode #IsSebsequenceProblem
To view or add a comment, sign in
-
-
Day 14 of LeetCode — Longest Common Prefix Today I solved the classic problem: finding the longest common prefix among a list of strings. Approach I used: Start with the first string as the prefix Compare it with each string in the array Shrink the prefix until it matches the start of every string If it becomes empty → no common prefix Time Complexity: O(n * m) (n = number of strings, m = length of prefix) This problem reinforced string manipulation and edge case handling (empty arrays, no matches). #Java #DSA #CodingJourney #LeetCode #ProblemSolving #TechGrowth
To view or add a comment, sign in
-
-
🚀 Day 48 of My #LeetCode Journey Today’s problem: 2615. Sum of Distances 💡 Key Idea: Instead of calculating distances between equal elements using brute force (O(n²)), I used: HashMap to group indices of same values Prefix Sum to efficiently compute distances This reduced the complexity to O(n) 🔥 🧠 What I Learned: How prefix sums can optimize distance calculations Efficient handling of repeated elements Writing clean and optimized code using Java ⚡ Approach: Store indices of each number Use prefix sums to calculate left & right distances Combine both to get final answer 📈 Time Complexity: O(n) 📦 Space Complexity: O(n) Consistency is key. Small progress every day leads to big results 💪 #Day48 #Java #FullStackDeveloper #CodingJourney #100DaysOfCode #DSA #LeetCode
To view or add a comment, sign in
-
💻 Day 35 of #LeetCode Journey 🔥 Solved: 19. Remove Nth Node From End of List Today’s problem was all about mastering Linked Lists and understanding the power of the Two Pointer technique. 🔍 Key Idea: Instead of calculating the length, I used a smart approach with fast and slow pointers. Move the fast pointer n steps ahead Then move both pointers together This helps locate the node to remove in a single pass ⚡ Why this approach? Efficient: O(n) time complexity No extra space required Clean and optimal solution 🧠 What I learned: Using a dummy node simplifies edge cases (like removing the head) Two-pointer technique is very powerful in linked list problems 📌 Problem Link: https://lnkd.in/gxXDR-YV #Java #DataStructures #LinkedList #CodingJourney #100DaysOfCode #LeetCode #ProblemSolving
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 68 — LeetCode Progress Problem: Check if an array is “special” — meaning every pair of adjacent elements has different parity (one even, one odd). Required: Given an integer array, return true if no two adjacent elements have the same parity, otherwise return false. Idea: If two neighboring numbers are both even or both odd, the condition fails immediately. So just compare parity of adjacent elements throughout the array. Approach: Traverse the array starting from index 1 For each element: Compare it with the previous element If both have same parity → return false If the loop completes → return true Time Complexity: O(n) Space Complexity: O(1) Clean, simple, and a good reminder that not every problem needs something fancy — sometimes it’s just about spotting the pattern. #LeetCode #DSA #Java #Arrays #ProblemSolving #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 58 of #100DaysOfCode Solved 165. Compare Version Numbers on LeetCode 🔗 🧠 Key Insight: Version strings are split by "." into multiple revisions. We compare each revision numerically (not lexicographically). Example: "1.01" = "1.1" (leading zeros don’t matter) ⚙️ Approach (Split + Compare): 1️⃣ Split both versions using "." 🔹 version1 → s1[] 🔹 version2 → s2[] 2️⃣ Traverse till max length of both arrays 3️⃣ For each index i: 🔹 num1 = i < s1.length ? parseInt(s1[i]) : 0 🔹 num2 = i < s2.length ? parseInt(s2[i]) : 0 4️⃣ Compare: 🔹 if num1 < num2 → return -1 🔹 if num1 > num2 → return 1 5️⃣ If all equal → return 0 ⏱️ Time Complexity: O(n + m) 📦 Space Complexity: O(n + m) (for split arrays) #100DaysOfCode #LeetCode #DSA #Strings #TwoPointers #Java #InterviewPrep #CodingJourney
To view or add a comment, sign in
-
-
🚀 LeetCode Challenge 3/50 💡 Approach: Two Pointer (In-Place) The straightforward way uses an extra array — but the problem specifically says no copying! So I used the Two Pointer technique to solve it in-place with a single pass. 🔍 Key Insight: → Use a 'left' pointer to track where the next non-zero element belongs → Traverse with 'right' pointer — place non-zeros at left, then increment → Fill remaining positions with 0s at the end 📈 Complexity: ✅ Time: O(n) — single pass ✅ Space: O(1) — no extra array, truly in-place Sometimes the constraint IS the optimization. Working within limits pushes us to think smarter! 🧠 #LeetCode #DSA #TwoPointer #Java #ADA #PBL2 #LeetCodeChallenge #Day3of50 #CodingJourney #ComputerEngineering #AlgorithmDesign #MoveZeroes
To view or add a comment, sign in
-
-
Day 56 : Crushing Binary Trees on LeetCode 💡 Today’s live practical session in Alpha Plus 7.0 We focused on some of the most challenging Binary Tree questions on LeetCode, breaking down the logic behind them. What I practiced today: ✅ Tree Diameter: understand the logic to calculate the absolute longest path between any two nodes in the entire tree. ✅ Maximum Path Sum: Tackled a famous "Hard" level problem! Figured out how to find the path with the highest possible sum, even if it doesn't pass through the root. ✅ Target Deletion: Wrote the recursive code to systematically find and delete leaf nodes that match a specific target value. #BinaryTree #LeetCode #ProblemSolving #DSA #Java #SoftwareEngineering #100DaysOfCode #ApnaCollege
To view or add a comment, sign in
-
-
🔥 𝗗𝗮𝘆 𝟴𝟵/𝟭𝟬𝟬 — 𝗟𝗲𝗲𝘁𝗖𝗼𝗱𝗲 𝗖𝗵𝗮𝗹𝗹𝗲𝗻𝗴𝗲 𝟳𝟰𝟰. 𝗙𝗶𝗻𝗱 𝗦𝗺𝗮𝗹𝗹𝗲𝘀𝘁 𝗟𝗲𝘁𝘁𝗲𝗿 𝗚𝗿𝗲𝗮𝘁𝗲𝗿 𝗧𝗵𝗮𝗻 𝗧𝗮𝗿𝗴𝗲𝘁 | 🟢 𝗘𝗮𝘀𝘆 | 𝗝𝗮𝘃𝗮 Looks easy — but the circular wrap-around is the trap most people miss. 𝗧𝗵𝗲 𝗽𝗿𝗼𝗯𝗹𝗲𝗺: Given a sorted circular array of letters, find the smallest letter strictly greater than the target. If none exists, wrap around to the first letter. 𝗔𝗽𝗽𝗿𝗼𝗮𝗰𝗵 — 𝗕𝗶𝗻𝗮𝗿𝘆 𝗦𝗲𝗮𝗿𝗰𝗵: ✅ Standard binary search for the first letter > target ✅ If letters[mid] > target → go left (end = mid - 1) ✅ Else → go right (start = mid + 1) ✅ After the loop, start points to the answer ✅ Use start % letters.length to handle the circular wrap-around 𝗧𝗵𝗲 𝗰𝗹𝗲𝘃𝗲𝗿 𝗯𝗶𝘁: If target is greater than or equal to all letters, start lands at letters.length — modulo wraps it back to index 0 automatically. No special case needed. 🔄 𝗖𝗼𝗺𝗽𝗹𝗲𝘅𝗶𝘁𝘆: ⏱ Time: O(log n) 📦 Space: O(1) One modulo operation handles the entire circular edge case cleanly. This is binary search with a twist — and the wrap-around trick is worth remembering! 🧠 📂 𝗙𝘂𝗹𝗹 𝘀𝗼𝗹𝘂𝘁𝗶𝗼𝗻 𝗼𝗻 𝗚𝗶𝘁𝗛𝘂𝗯: https://lnkd.in/gvK_p44b 11 more days. So close! 💪 #LeetCode #Day89of100 #100DaysOfCode #Java #DSA #BinarySearch #Arrays #CodingChallenge #Programming
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