Under construction

Still being built. Things may change

Counting Special Integer Pairs

Interview

Time limit1sMemory limit512 MB

Summary
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 AA and BB of length NN arbitrarily. Then for each pair of integers 1≤i<j≤N1 \le i \lt j \le N, he defines

X(i,j)=Ai−AjX(i, j) = A_i - A_j, Y(i,j)=Bi−BjY(i, j) = B_i - B_j, Z(i,j)=∣X(i,j)−Y(i,j)∣Z(i, j) = |X(i, j) - Y(i, j)|

Using ZZ, he defines C(D)=∣{(i,j):Z(i,j)≤D}∣C(D) = |\{(i, j) : Z(i, j) \le D\}|. In other words, C(D)C(D) is the number of special pairs (i,j)(i, j) whose Z(i,j)Z(i, j) is at most DD. Only pairs with 1≤i<j≤N1 \le i \lt j \le N are considered.

Computing C(D)C(D) from given AA, BB, and DD is easy, so Albert came up with a new game. Given AA, BB, and an integer KK, determine whether some DD satisfies C(D)=KC(D) = K. If one exists, find the smallest such DD.

Input

The first line contains the number of test cases TT. The first line of each test case contains NN and KK. The second line contains the NN integers of array AA, separated by spaces. The third line contains the NN integers of array BB, separated by spaces.

Output

For each test case, print the answer on its own line. If no DD satisfies the condition, print -1.

Constraints

  • 1≤T≤51 \le T \le 5
  • 2≤N≤500002 \le N \le 50000
  • 0≤K≤N×(N−1)/20 \le K \le N \times (N-1) / 2
  • For all ii with 1≤i≤N1 \le i \le N: 0≤Ai,Bi≤1050 \le A_i, B_i \le 10^5

Examples2

  1. Example 1

    Input
    5
    5 2
    2 4 6 4 2
    9 1 3 7 5
    5 4
    2 4 6 4 2
    9 1 3 7 5
    5 6
    2 4 6 4 2
    9 1 3 7 5
    5 8
    2 4 6 4 2
    9 1 3 7 5
    5 10
    2 4 6 4 2
    9 1 3 7 5
    
    Expected output
    0
    4
    -1
    6
    10
    
  2. Example 2

    Input
    1
    9 18
    1 2 3 4 5 4 3 2 1
    5 4 3 2 1 2 3 4 5
    
    Expected output
    2