Maximum Distance
InterviewTime limit1sMemory limit128 MB
Given two non-increasing arrays, find the largest j - i such that j >= i and Y[j] >= X[i].
- Level
Medium4 of 10
- Topics
- Array, Two pointers, Greedy, Sorting
- Solved
- No attempts yet
Problem
Consider two non-increasing integer sequences and , where and for all .
The distance between two elements and is if and , and otherwise.
The distance between the sequences and is
For example, for the sequences and shown below, the maximum is attained at and , so .

Input
The first line contains the number of test cases . Each test case consists of three lines: the first line contains the sequence length (); the second line contains the elements of sequence separated by spaces; the third line contains the elements of sequence separated by spaces. Both sequences are non-increasing and have equal length.
Output
For each test case, print a single line The maximum distance is d, where is the value of . Separate the output of consecutive test cases with one blank line.