Max Distance in Sorted Arrays with Two Pointers

🚀 LeetCode Day Problem Solving 🚀 Day-53 📌 Problem: You are given two non-increasing arrays nums1 and nums2. A pair (i, j) is valid if: ✔ i ≤ j ✔ nums1[i] ≤ nums2[j] 👉 Distance = j - i Your task is to find the maximum distance among all valid pairs. 🧠 Example: Input: nums1 = [55,30,5,4,2] nums2 = [100,20,10,10,5] ✅ Output: 2 📖 Explanation: Valid pairs include (2,4) → distance = 4 - 2 = 2 👉 This is the maximum possible distance. 💡 Key Insight: ✔ Arrays are already sorted (non-increasing) ✔ Use Two Pointer Technique 👉 Start with i = 0, j = 0 If nums1[i] ≤ nums2[j] → valid pair → update answer → move j++ Else → move i++ ⚡ This avoids checking all pairs (which would be slow). 📊 Complexity Analysis: ⏱ Time Complexity: O(n + m) 📦 Space Complexity: O(1) 🧠 What I Learned: ✔ Efficient use of Two Pointers ✔ Leveraging sorted properties ✔ Avoiding brute force with smart traversal ✅ Day 53 Completed 🚀 Leveling up in Arrays + Greedy + Two Pointers 💪 #Leetcode #DSA #ProblemSolving #BitManipulation #CodingJourney #InterviewPreparation #Consistency #MilanSahoo 🚀

  • graphical user interface, text, application, email

To view or add a comment, sign in

Explore content categories