2 ms faster than 99.57% Java solution of O(n) time and O(1) space
Traversing from the right to the left, let \n i be the pointer in nums1 and \n j be the pointer of the rightmost index in nums2 that is larger than nums1[i].\n\
Traversing from the right to the left, let \n* `i` be the pointer in `nums1` and \n* `j` be the pointer of the rightmost index in `nums2` that is larger than `nums1[i]`.\n\nWhile we move `i` from the right to the left, we adjust `j` accordingly. The difference of `j - i` is the furthest pair for the element `nums1[i]`.\n\nPlease upvote if you like it. Thank you! \uD83D\uDE0A\n\n```\nclass Solution {\n  public int maxDistance(int[] nums1, int[] nums2) {\n    int res = 0;\n    int j = nums2.length - 1;\n    for (int i = nums1.length - 1; i != -1; i--) {\n      while (j >= i && nums2[j] < nums1[i])\n        j--;\n      res = Math.max(res, j - i);\n    }\n    return res;\n  }\n}\n```
['Two Pointers', 'Java']