Minimum Adjacent Difference

3.3

6 votes
Introduction to Dynamic Programming 1, Algorithms, Dynamic Programming, Binary Search
Problem

You are given two arrays A,B, each containing N integers. You want to minimize the maximum absolute difference between two adjacent integers of the array A (i.e. maxN1i=1AiAi+1). You can do the following operation atmost K times:

  • Choose an index i(1iN) and swap Ai,Bi.

Find the minimum possible value of maxN1i=1AiAi+1.

  Input format

  • The first line contains T denoting the number of test cases. The description of T test cases is as follows:
  • For each test case:
    • The first line contains two integers N,K denoting the size of array A and the maximum number of operations to be performed respectively.
    • The second line contains N space-separated integers A1,A2,,AN - denoting the elements of the array A.
    • The third line contains N space-separated integers B1,B2,,BN - denoting the elements of the array B.

Output format
For each test case, print  the minimum possible value of maxN1i=1AiAi+1.

Constraints

1T1042N1051KN1Ai109SumofNoveralltestcasesdoesnotexceed2105.

 

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

In the first test case, applying the operation on index i = 2 makes A = [5, 3]. Thus the answer is |5 - 3| = 2.

In the second test case, applying the operation on index i = 2 and index i =  4 makes A = [5, 7, 4, 3]. Thus the answer is max(|5 - 7|, |7 - 4|, |4 - 3|) = max(2, 3, 1) = 3.

Editor Image

?