🚀 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
Max Distance Between Sorted Array Elements
More Relevant Posts
-
🚀 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 571 of #750DaysOfCode 🚀 🔍 Problem Solved: Sum of Distances Today’s problem looked like a classic brute-force trap 👀 At first glance, comparing every pair gives an O(n²) solution — but with constraints up to 10⁵, that’s not going to work. 💡 Key Insight: Instead of comparing all pairs, we can: 👉 Group indices of the same value 👉 Use prefix sums to efficiently calculate distances 🧠 Approach: Group indices by value (using HashMap) For each group: Build prefix sum of indices For each index: Left contribution → i * count - sum Right contribution → sum - i * count Combine both to get final result 📈 Complexity: Time: O(n) Space: O(n) ✨ Takeaway: When you see distance-based problems: 👉 Think in terms of contributions instead of pair comparisons 👉 Prefix sums can turn expensive computations into linear time Another strong pattern added to the toolkit 💪 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #PrefixSum #Algorithms #LearningEveryday
To view or add a comment, sign in
-
-
Day 103: Simple & Clean 🎯 Problem 1848: Minimum Distance to the Target Element After some complex DP challenges, today was a straightforward exercise in linear search and distance calculation. The Strategy: • Linear Traversal: I iterated through the array to find every occurrence of the target element. • Absolute Minimization: For each match, I calculated the absolute difference between the current index and the start index, keeping track of the minimum value found. Sometimes a simple, O(N) solution is all you need. Day 103 down—maintaining the streak with clarity and consistency. 🚀 #LeetCode #Java #Algorithms #ProblemSolving #DailyCode
To view or add a comment, sign in
-
-
💡 Day 57 of LeetCode Problem Solved! 🔧 🌟 Maximum Distance Between a Pair of Values 🌟 🔗 Solution Code: https://lnkd.in/gSP_QePE 🧠 Approach: • Two-Pointer Strategy: Maintain two indices (i for nums1 and j for nums2) to scan both non-increasing arrays in a single efficient pass. • Greedy Search: If nums1[i] <= nums2[j], calculate j - i and expand j to find the maximum possible distance. If the value in nums1 is too large, move i forward. ⚡ Key Learning: • Leveraging the non-increasing nature of arrays with two pointers eliminates the need for nested loops, reducing complexity from O(N^2) to linear O(N+M). ⏱️ Complexity: • Time: O(n + m) • Space: O(1) #LeetCode #Java #DSA #ProblemSolving #Consistency #100DaysOfCode #CodingJourney #Algorithms #TwoPointers
To view or add a comment, sign in
-
-
Day 12 of #100DaysOfCode — Sliding Window Today, I worked on the problem “Max Consecutive Ones III” LeetCode. Problem Summary Given a binary array, the goal is to find the maximum number of consecutive 1s if you can flip at most k zeros. Approach At first glance, this problem looks like a brute-force or restart-based problem, but the optimal solution lies in the Sliding Window technique. The key idea is to maintain a window [i, j] such that: The number of zeros in the window does not exceed k Expand the window by moving j Shrink the window by moving i whenever the constraint is violated Instead of restarting the window when the condition breaks, we dynamically adjust it. Key Logic Traverse the array using pointer j Count the number of zeros in the current window If zeros exceed k, move pointer i forward until the window becomes valid again At every step, update the maximum window size Why This Works This approach ensures: Each element is processed at most twice Time Complexity: O(n) Space Complexity: O(1) The most important learning here is understanding how to dynamically adjust the window instead of resetting it, which is a common mistake while applying sliding window techniques. In sliding window problems, always focus on expanding and shrinking the window efficiently rather than restarting the computation. #100DaysOfCode #DSA #SlidingWindow #LeetCode #Java #ProblemSolving #CodingJourney #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 74/100 – LeetCode Challenge 🔍 Problem Solved: Find the Duplicate Number (287) Today’s problem was a great reminder that sometimes the best solutions come from thinking differently 💡 Instead of using extra space or modifying the array, I used Floyd’s Cycle Detection Algorithm (Tortoise & Hare) — treating the array like a linked list to detect a cycle. 👉 Key Learnings: • Arrays can sometimes be visualized as linked structures • Cycle detection is not just for linked lists! • Optimizing for O(1) space is a common interview expectation ⚡ Approach: Use two pointers (slow & fast) First, find intersection point Then, find the cycle start → duplicate number ⏱ Time Complexity: O(n) 📦 Space Complexity: O(1) ✅ Successfully passed all test cases! Consistency > Perfection. Let’s keep going 💪 #Day74 #LeetCode #100DaysOfCode #Java #CodingInterview #ProblemSolving #DataStructures #Algorithms
To view or add a comment, sign in
-
-
🚀 Day 30 of #100DaysOfCode Solved Trapping Rain Water 🌧️ One of those classic problems that really tests your ability to think beyond brute force. Instead of checking every pair, I used the prefix max (leftMax) and suffix max (rightMax) approach to efficiently calculate trapped water. 🔍 Key Takeaways: Precomputing left and right maximum heights simplifies the problem Water trapped at each index = min(leftMax, rightMax) − height[i] Turning intuition into optimized code is the real win here 💻 Complexity: Time: O(n) Space: O(n) Another step forward in mastering arrays and patterns 💪 #DSAwithEdSlash #LeetCode #Java #ProblemSolving #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
Day 36/50 🚀 Solved “Valid Parentheses” today — a classic stack problem, but a great reminder that simple concepts can be powerful when applied correctly. 💡 Key takeaway: Using a stack makes it easy to track opening brackets and validate matching pairs efficiently. Every closing bracket should correspond to the most recent unmatched opening one — LIFO in action. ⚙️ What I focused on: Clean conditional checks Avoiding unnecessary complexity Writing readable, structured code 📈 Result: Accepted ✅ Optimized runtime & solid performance On to Day 37 🔥 #50DaysOfCode #DataStructures #Algorithms #Java #LeetCode #CodingJourney
To view or add a comment, sign in
-
-
🚀 Day 563 of #750DaysOfCode 🚀 📌 Problem Solved: Shortest Distance to Target String in a Circular Array Today I explored a clean and optimized approach to solving a circular array problem 🔄 💡 Key Insight: Instead of checking all indices, we can expand from the start index in both directions simultaneously 👉 At each step i, we check: Forward → (start + i) % n Backward → (start - i + n) % n ⏱️ The moment we find the target, we return i → which is guaranteed to be the minimum distance 🧠 Why this works: We are exploring layer by layer (like BFS on array) First match = shortest path ✅ No need to scan entire array unnecessarily 🔥 What I Learned: Circular problems can often be solved using modulo arithmetic Expanding outward is more efficient than brute force Think in terms of minimum steps, not positions Consistency is the real game changer 💯 On to Day 564 🚀 #LeetCode #Java #Algorithms #DataStructures #CodingJourney #ProblemSolving #Consistency
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
-
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