All problems

Problem 575

Maximum Distance Between a Pair of Values

Medium · 20 pts · Array, Two Pointers, Binary Search

Problem

You are given two non-increasing arrays A (length N) and B (length M). A pair of indices (i, j) is valid if i <= j and A[i] <= B[j]; its distance is j - i. Find the largest distance of any valid pair, or 0 if there is none. Because both arrays are non-increasing, two pointers work: advance j while the pair is valid, recording the distance, and advance i when it is not.

Input

The first line contains N and M. The second line contains A. The third line contains B. Both arrays are non-increasing.

Output

Print the maximum distance of a valid pair, or 0.

Constraints

1 <= N, M <= 100000; 1 <= A[i], B[j] <= 100000

Example input

5 5
55 30 5 4 2
100 20 10 10 5

Example output

2

Explanation

With A = 55 30 5 4 2 and B = 100 20 10 10 5, the pair (2, 4) is valid because A[2] = 5 <= B[4] = 5, and its distance is 2. No valid pair is farther apart, so the answer is 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Successful Pairs of Spells and Potions Next: Find the Distance Value Between Two Arrays