O(n) 2 pointers
Since our arrays are sorted, we can advance i while n1[i] is bigger than n2[j], and increment j otherwise.\n\nJava\njava\npublic int maxDistance(int[] n1, int[]
Since our arrays are sorted, we can advance `i` while `n1[i]` is bigger than `n2[j]`, and increment `j` otherwise.\n\n**Java**\n```java\npublic int maxDistance(int[] n1, int[] n2) {\n    int i = 0, j = 0, res = 0;\n    while (i < n1.length && j < n2.length)\n        if (n1[i] > n2[j])\n            ++i;\n        else\n            res = Math.max(res, j++ - i);\n    return res;\n}\n```\n**C++**\n```cpp\nint maxDistance(vector<int>& n1, vector<int>& n2) {\n    int i = 0, j = 0, res = 0;\n    while (i < n1.size() && j < n2.size())\n        if (n1[i] > n2[j])\n            ++i;\n        else\n            res = max(res, j++ - i);\n    return res;\n}\n```\n**Python 3**\n```python\nclass Solution:\n    def maxDistance(self, n1: List[int], n2: List[int]) -> int:\n        i = j = res = 0\n        while i < len(n1) and j < len(n2):\n            if n1[i] > n2[j]:\n                i += 1\n            else:\n                res = max(res, j - i)\n                j += 1\n        return res\n```
['C', 'Python', 'Java']