Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two non-increasing 0-indexed integer arrays nums1 and nums2.
A pair of indices (i, j), where 0 <= i < nums1.length and 0 <= j < nums2.length, is valid if both i <= j and nums1[i] <= nums2[j]. The distance of the pair is j - i.
Return the maximum distance of any valid pair (i, j). If there are no valid pairs, return 0.
An array arr is non-increasing if arr[i-1] >= arr[i] for every 1 <= i < arr.length.
Example 1:
Input: nums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5] Output: 2 Explanation: The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4). The maximum distance is 2 with pair (2,4).
Example 2:
Input: nums1 = [2,2,2], nums2 = [10,10,1] Output: 1 Explanation: The valid pairs are (0,0), (0,1), and (1,1). The maximum distance is 1 with pair (0,1).
Example 3:
Input: nums1 = [30,29,19,5], nums2 = [25,25,25,25,25] Output: 2 Explanation: The valid pairs are (2,2), (2,3), (2,4), (3,3), and (3,4). The maximum distance is 2 with pair (2,4).
Constraints:
1 <= nums1.length, nums2.length <= 1051 <= nums1[i], nums2[j] <= 105nums1 and nums2 are non-increasing.Problem summary: You are given two non-increasing 0-indexed integer arrays nums1 and nums2. A pair of indices (i, j), where 0 <= i < nums1.length and 0 <= j < nums2.length, is valid if both i <= j and nums1[i] <= nums2[j]. The distance of the pair is j - i. Return the maximum distance of any valid pair (i, j). If there are no valid pairs, return 0. An array arr is non-increasing if arr[i-1] >= arr[i] for every 1 <= i < arr.length.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search
[55,30,5,4,2] [100,20,10,10,5]
[2,2,2] [10,10,1]
[30,29,19,5] [25,25,25,25,25]
two-furthest-houses-with-different-colors)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1855: Maximum Distance Between a Pair of Values
class Solution {
public int maxDistance(int[] nums1, int[] nums2) {
int ans = 0;
int m = nums1.length, n = nums2.length;
for (int i = 0; i < m; ++i) {
int left = i, right = n - 1;
while (left < right) {
int mid = (left + right + 1) >> 1;
if (nums2[mid] >= nums1[i]) {
left = mid;
} else {
right = mid - 1;
}
}
ans = Math.max(ans, left - i);
}
return ans;
}
}
// Accepted solution for LeetCode #1855: Maximum Distance Between a Pair of Values
func maxDistance(nums1 []int, nums2 []int) int {
ans, n := 0, len(nums2)
for i, num := range nums1 {
left, right := i, n-1
for left < right {
mid := (left + right + 1) >> 1
if nums2[mid] >= num {
left = mid
} else {
right = mid - 1
}
}
if ans < left-i {
ans = left - i
}
}
return ans
}
# Accepted solution for LeetCode #1855: Maximum Distance Between a Pair of Values
class Solution:
def maxDistance(self, nums1: List[int], nums2: List[int]) -> int:
ans = 0
nums2 = nums2[::-1]
for i, v in enumerate(nums1):
j = len(nums2) - bisect_left(nums2, v) - 1
ans = max(ans, j - i)
return ans
// Accepted solution for LeetCode #1855: Maximum Distance Between a Pair of Values
impl Solution {
pub fn max_distance(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let m = nums1.len();
let n = nums2.len();
let mut res = 0;
for i in 0..m {
let mut left = i;
let mut right = n;
while left < right {
let mid = left + (right - left) / 2;
if nums2[mid] >= nums1[i] {
left = mid + 1;
} else {
right = mid;
}
}
res = res.max((left - i - 1) as i32);
}
res
}
}
// Accepted solution for LeetCode #1855: Maximum Distance Between a Pair of Values
function maxDistance(nums1: number[], nums2: number[]): number {
let ans = 0;
let m = nums1.length;
let n = nums2.length;
for (let i = 0; i < m; ++i) {
let left = i;
let right = n - 1;
while (left < right) {
const mid = (left + right + 1) >> 1;
if (nums2[mid] >= nums1[i]) {
left = mid;
} else {
right = mid - 1;
}
}
ans = Math.max(ans, left - i);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.