Counting Special Integer Pairs
InterviewTime limit1sMemory limit512 MB
Given arrays A and B, find the smallest D where the number of pairs with Z(i, j) at most D equals K, or report -1 if none exists.
- Level
Medium5 of 10
- Topics
- Binary search, Sorting, Two pointers
- Solved
- No attempts yet
Statement
Albert enjoys a game of counting special integer pairs using integer arrays. First he chooses two integer arrays and of length arbitrarily. Then for each pair of integers , he defines
, ,
Using , he defines . In other words, is the number of special pairs whose is at most . Only pairs with are considered.
Computing from given , , and is easy, so Albert came up with a new game. Given , , and an integer , determine whether some satisfies . If one exists, find the smallest such .
Input
The first line contains the number of test cases . The first line of each test case contains and . The second line contains the integers of array , separated by spaces. The third line contains the integers of array , separated by spaces.
Output
For each test case, print the answer on its own line. If no satisfies the condition, print -1.
Constraints
- For all with :