Maximum Distance Between a Pair of Values

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
Inputnums1 = [55,30,5,4,2], nums2 = [100,20,10,10,5]
Output2
The valid pairs are (0,0), (2,2), (2,3), (2,4), (3,3), (3,4), and (4,4), and the maximum distance is 2 with pair (2,4).
Example 2
Inputnums1 = [2,2,2], nums2 = [10,10,1]
Output1
The valid pairs are (0,0), (0,1), and (1,1), and the maximum distance is 1 with pair (0,1).

Constraints

  • 1 <= nums1.length, nums2.length <= 10^5
  • 1 <= nums1[i], nums2[j] <= 10^5
  • Both nums1 and nums2 are non-increasing.

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate