Maximum Distance Between a Pair of Values LeetCode solution in JavaScript
1var maxDistance = function (nums1, nums2) {2let i = 0,3j = 0;45let ans = 0;6while (i < nums1.length && j < nums2.length) {7// maintain the i <= j invariant8j = Math.max(j, i);910// we want to maximize j so move it forward whenever possible11while (nums1[i] <= nums2[j]) {12ans = Math.max(ans, j - i);13j++;14}1516// we want to minimize i so move it forward only to maintain invariants17i++;18}1920return ans;21};