Python Solution || Space complexity better than 98% of submissions
\nclass Solution:\n    def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:\n        #\xA0Worst case, we walk through both arrays O(N + M) ~ Time c
```\nclass Solution:\n    def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:\n        #\xA0Worst case, we walk through both arrays O(N + M) ~ Time complexity\n        #\xA0Space complexity O(1) as we only allocate 3 free variables\n        p1, p2 = 0, 0\n\n        maxDiff = 0\n\n        while p1 < len(nums1) and p2 < len(nums2):\n\n            if nums1[p1] <= nums2[p2]:\n                # Correct solution\n                maxDiff = max(maxDiff, p2 - p1)\n                #\xA0increment p2\n                p2 += 1\n\n            elif p1 == p2:\n                # Event of nums2 being smaller, we need to walk both pointers\n                p1 += 1\n                p2 += 1\n            elif p1 < p2:\n                #\xA0walk only p1 because its behind\n                p1 += 1\n\n        return maxDiff\n```
['Two Pointers']