Max Distance Between Two Sorted Arrays Using Binary Search

Maximum distance between a pair of values Given nums1 and nums2 🎯Target: Max distance (j - i) such that i <= j and nums1[i] <= nums2[j] Thought process... We'll iterate over nums1 and for each index i, we need to find a valid j in nums2. At first, can think of linear search… but wait 🤔 Both arrays are sorted in non-increasing order, so we can do better Instead of scanning, we use binary search on nums2 to find the rightmost index j such that: nums2[j] >= nums1[i] j found, then compute j - i and update our answer. And yeah… that’s pretty much it. Clean and efficient😄 #potd #DSA #Algorithms #BinarySearch #problemSolving

  • text

Nice 👍 Khushboo . You can also think it with a two pointer approach(as arrays are already sorted), it will optimize your current O(n*logm) complexity to O(n+m).

To view or add a comment, sign in

Explore content categories