[Java/C++/Python] 2 Pointers,  3 Solutions
Solution 1\nIterate on input array B\n\nTime O(n + m)\nSpace O(1)\n\nJava\njava\n    public int maxDistance(int[] A, int[] B) {\n        int res = 0, i = 0, n =
# Solution 1\nIterate on input array `B`\n\nTime `O(n + m)`\nSpace `O(1)`\n\n**Java**\n```java\n    public int maxDistance(int[] A, int[] B) {\n        int res = 0, i = 0, n = A.length, m = B.length;\n        for (int j = 0; j < m; ++j) {\n            while (i < n && A[i] > B[j])\n                i++;\n            if (i == n) break;\n            res = Math.max(res, j - i);\n        }\n        return res;\n    }\n```\n\n**C++**\n```cpp\n    int maxDistance(vector<int>& A, vector<int>& B) {\n        int res = 0, i = 0, n = A.size(), m = B.size();\n        for (int j = 0; j < m; ++j) {\n            while (i < n && A[i] > B[j])\n                i++;\n            if (i == n) break;\n            res = max(res, j - i);\n        }\n        return res;\n    }\n```\n\n**Python**\n```py\n    def maxDistance(self, A, B):\n        res = i = 0\n        for j, b in enumerate(B):\n            while i < len(A) and A[i] > b:\n                i += 1\n            if i == len(A): break\n            res = max(res, j - i)\n        return res\n```\n<br>\n\n# Solution 2\nIterate on input array `A`\n\nTime `O(n + m)`\nSpace `O(1)`\n\n**Java**\n```java\n    public int maxDistance(int[] A, int[] B) {\n        int res = 0, j = -1, n = A.length, m = B.length;\n        for (int i = 0; i < n; ++i) {\n            while (j + 1 < m && A[i] <= B[j + 1])\n                j++;\n            res = Math.max(res, j - i);\n        }\n        return res;\n    }\n```\n**C++**\n```cpp\n    int maxDistance(vector<int>& A, vector<int>& B) {\n        int res = 0, j = -1, n = A.size(), m = B.size();\n        for (int i = 0; i < n; ++i) {\n            while (j + 1 < m && A[i] <= B[j + 1])\n                j++;\n            res = max(res, j - i);\n        }\n        return res;\n    }\n```\n**Python**\n```py\n    def maxDistance(self, A, B):\n        res, j = 0, -1\n        for i, a in enumerate(A):\n            while j + 1 < len(B) and a <= B[j + 1]:\n                j += 1\n            res = max(res, j - i)\n        return res\n```\n\n# Solution 3\nIterate on input array `A` and `B`\n\nTime `O(n + m)`\nSpace `O(1)`\n\n**Java**\n```java\n    public int maxDistance(int[] A, int[] B) {\n        int i = 0, j = 0, res = 0, n = A.length, m = B.length;\n        while (i < n && j < m) {\n            if (A[i] > B[j])\n                i++;\n            else\n                res = Math.max(res, j++ - i);\n        }\n        return res;\n    }\n```\n**C++**\n```cpp\n    int maxDistance(vector<int>& A, vector<int>& B) {\n        int i = 0, j = 0, res = 0, n = A.size(), m = B.size();\n        while (i < n && j < m) {\n            if (A[i] > B[j])\n                i++;\n            else\n                res = max(res, j++ - i);\n        }\n        return res;\n    }\n```
[]