🚀 Day 565 of #750DaysOfCode 🚀 🔍 Problem Solved: Minimum Absolute Distance Between Mirror Pairs Today’s problem was all about identifying mirror pairs in an array — where reversing one number equals another — and finding the minimum index distance between such pairs. 💡 Key Insight: Instead of brute force (O(n²)), we can optimize using a HashMap to track previously seen reversed values. 🧠 Approach: Traverse the array once For each number: Check if it already exists in the map → update minimum distance Compute its reverse Store the reversed number with its index Return the minimum distance, or -1 if no pair exists 📊 Complexity: Time: O(n × d) → d = number of digits Space: O(n) 🔥 Takeaway: Using a reverse transformation + hashmap lookup converts a nested loop problem into a linear scan — a classic optimization pattern! #Day565 #750DaysOfCode #LeetCode #Java #DataStructures #Algorithms #CodingJourney #HashMap #ProblemSolving
Minimum Absolute Distance Between Mirror Pairs in Array
More Relevant Posts
-
First Missing Positive — Not as Simple as It Looks At first glance, this problem looks simple. A natural instinct is to solve it using a hashmap or a set to track elements. But that approach won’t work here. The problem strictly requires: O(n) Time Complexity O(1) Space Complexity So we need a different way of thinking. The key idea is to use the input array itself as a marker instead of extra space. Approach: Clean the array Replace all negative numbers, zeros, and values greater than n with n + 1 Because the answer will always lie in the range [1, n+1] Mark presence using indices For every number num, mark the index num - 1 as negative This indicates that the number exists in the array Identify the missing number The first index that has a positive value gives the answer (index + 1) Edge case If all indices are marked, then the answer is n + 1 Why this works: We reuse the given array, so no extra space is needed. Each element is processed a constant number of times, ensuring linear time. Insight: This problem shows how constraints change your approach. A hashmap solution is straightforward, but it violates the space requirement. Sometimes the optimal solution comes from rethinking how to use the existing data instead of adding new structures. #DataStructures #Algorithms #Java #CodingInterview #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 567 of #750DaysOfCode 🚀 🔍 Problem Solved: Maximum Distance Between a Pair of Values Today’s challenge was about finding the maximum distance (j - i) such that: ✔️ i ≤ j ✔️ nums1[i] ≤ nums2[j] ✔️ Both arrays are non-increasing 💡 Key Insight: Since both arrays are sorted in descending order, we can avoid brute force and use a Two Pointer approach to achieve optimal performance. 🧠 Approach: Initialize two pointers i and j at 0 If nums1[i] ≤ nums2[j] → valid pair → update distance & move j Else → move i forward Maintain j ≥ i at all times 📊 Complexity: Time: O(n + m) Space: O(1) 🔥 Takeaway: Whenever arrays are sorted, always think of two pointers or binary search before jumping to brute force. This simple shift can reduce complexity from O(n²) → O(n)! #Day567 #750DaysOfCode #LeetCode #Java #DataStructures #Algorithms #TwoPointers #CodingJourney #ProblemSolving
To view or add a comment, sign in
-
-
🚀 Day 566 of #750DaysOfCode 🚀 🔍 Problem Solved: Mirror Distance of an Integer Today’s problem was simple but a great reminder of how powerful basic number manipulation can be. 💡 Problem Insight: We define the mirror distance of a number as: 👉 abs(n - reverse(n)) So the task is: Reverse the digits of the number Compute the absolute difference 🧠 Approach: Extract digits using % 10 Build the reversed number Compute absolute difference 📊 Complexity: Time: O(d) → where d = number of digits Space: O(1) 🔥 Key Takeaway: Even the easiest problems reinforce fundamentals like digit extraction, reversal, and absolute difference — building blocks for more complex algorithms. #Day566 #750DaysOfCode #LeetCode #Java #CodingJourney #ProblemSolving #Algorithms #BeginnerFriendly
To view or add a comment, sign in
-
-
🌲 Day 38/75: Measuring the Diameter of a Binary Tree! Today’s LeetCode challenge was a step up in complexity: finding the Diameter of a Binary Tree. The diameter is the length of the longest path between any two nodes in a tree, which may or may not pass through the root. The Strategy: This problem is tricky because the longest path isn't always just the height of the left side plus the height of the right side at the root. It could be tucked away in one of the subtrees! I used a recursive Depth-First Search (DFS) that performs two tasks at once: Calculates Height: It returns the height of the current subtree to its parent. Updates Diameter: It calculates the path passing through the current node (leftHeight + rightHeight) and updates a global diameter variable if this path is the longest found so far. Performance: ✅ Runtime: 1 ms ✅ Complexity: $O(n)$ Time | $O(h)$ Space It’s a great example of how to extract multiple pieces of information from a single recursive pass. One more day closer to the 40-day mark! 🚀 #LeetCode #75DaysOfCode #Java #BinaryTree #Recursion #Algorithms #DataStructures #TechJourney
To view or add a comment, sign in
-
-
Worked on a challenging problem: “Subarrays with K Different Integers” Key takeaway: Instead of directly solving for exactly K distinct elements, I learned a smarter approach: 👉 count(at most K) − count(at most K−1) 🔹 Concepts I practiced: Sliding Window technique HashMap for frequency tracking Two-pointer approach 🔹 What stood out: The idea of counting all valid subarrays ending at each index using (r - l + 1) was really powerful. It completely changed how I think about subarray problems. Always learning, one problem at a time. #DataStructures #Algorithms #Java #LeetCode #SlidingWindow #LearningJourney #ProblemSolving
To view or add a comment, sign in
-
-
Day 09 of #50DaysOfLeetCode Challenge Just tackled the "Combination Sum" problem! This was a fantastic exercise in backtracking. The challenge is to find all unique combinations of numbers that sum up to a specific target, with the twist that you can use the same number multiple times. Key Insights: Backtracking Power: It’s all about exploring every possible path and "backtracking" as soon as the sum exceeds the target. State Space Tree: Visualizing how the recursion branches out helped me understand how to avoid duplicate combinations while allowing multiple uses of the same element. Decision Making: Learning when to include an element and when to move to the next index is crucial for optimizing the search. Each day, the logic gets sharper and the problems get more interesting! #DataStructures #Algorithms #CodingJourney #Java #Backtracking #LeetCode #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 #Day54 of #100DaysDSAChallenge Solved #LeetCode160: Intersection of Two Linked Lists The problem asks us to find the node where two singly linked lists intersect. 🔹 Approach: 🔸 Brute Force Store nodes of one list in a hashmap/set and check while traversing the second list. ⏱️ Time: O(n + m) | 📦 Space: O(n) 🔸 Better Find lengths of both lists, move the longer list ahead by the difference, then traverse together to find intersection. ⏱️ Time: O(n + m) | 📦 Space: O(1) 🔸 Optimal Use two pointers and switch heads when reaching null so both traverse equal distance and meet at intersection. ⏱️ Time: O(n + m) | 📦 Space: O(1) 💡 Great example of improving from extra space → alignment → elegant pointer trick. 🔗 Github repo: https://lnkd.in/g_rSFCh8 #100DaysOfDSA #DSA #LeetCode #Java #Algorithms #DataStructures #KunalKushwaha #Striver #GeeksForGeeks #ProblemSolving #CodingJourney #LearningInPublic #InterviewPrep #PlacementPreparation 🚀
To view or add a comment, sign in
-
-
Day 2/100: Mastering the Pivot 🔄 Today’s challenge was LeetCode 33: Search in Rotated Sorted Array. The trick here isn't just finding the target—it’s identifying which half of the array is still sorted. Standard Binary Search assumes a perfect slope, but with a rotation, you have to find the "steady ground" before making your move. Constraint: O(\log n) runtime. Key Lesson: Even when data is disrupted (rotated), there is usually a sub-pattern you can exploit to maintain efficiency. One step closer to the goal! 🚀 #100DaysOfCode #Java #DataStructures #Algorithms #LeetCode
To view or add a comment, sign in
-
-
#70DayStreak 🚀 27/04/26 — Optimized Sliding Window | Longest Substring Without Repeating Characters (LeetCode 3) Today I revisited a classic problem — Longest Substring Without Repeating Characters — and implemented a more optimized Sliding Window using HashMap. Previously, I used a HashSet approach where the window shrinks step-by-step. This time, I upgraded it with a "jumping window" technique, allowing direct pointer movement. 🔑 Key Optimization Insight Instead of removing characters one by one: • Store last seen index of each character • When duplicate appears → jump left pointer instantly • Avoid unnecessary iterations 👉 This reduces redundant work and improves efficiency significantly. ⚙️ Algorithm Highlights • Left pointer jumps using stored indices • Right pointer scans once • Continuous max window calculation 📊 Complexity • Time: O(n) • Space: O(min(m, n)) 📈 Performance • Runtime: 5ms (Beats 87.15%) • Memory: 45.67 MB (Beats 69.72%) 🔥 Consistency Metrics • 84 Active Days • 70-Day Streak 💡 Key Learning Switching from HashSet → HashMap transforms the approach from iterative shrinking to direct optimization. This is a small shift in thinking, but a big upgrade in performance. 📌 Even after 100+ problems, refinement and optimization still matter. #DSA #Java #LeetCode #SlidingWindow #HashMap #Algorithms #CodingJourney #ProblemSolving #Optimization #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 100 Days of Code Day-25 LeetCode Problem Solved: Reverse Nodes in k-Group I recently worked on a Linked List problem that focuses on reversing nodes in groups of size k while preserving the remaining structure if the group size is less than k. 🔹 Strengthened my understanding of pointer manipulation 🔹 Improved problem decomposition skills 🔹 Practiced recursive thinking for efficient implementation 💡 Key takeaway: Breaking complex problems into smaller, manageable parts significantly simplifies the solution approach. Continuing to build consistency in problem-solving and deepen my understanding of Data Structures & Algorithms. #LeetCode #DataStructures #Algorithms #Java #ProblemSolving #SoftwareDevelopment
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