Minimum Adjacent Difference
Practice
3.3 (6 votes)
Introduction to dynamic programming 1
Algorithms
Dynamic programming
Binary search
Problem
54% Success 658 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

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. \(\max_{i=1}^{ N-1} \mid A_i - A_{i+1} \mid\)). You can do the following operation atmost \(K\) times:

  • Choose an index \(i \;(1 \le i \le N)\) and swap \(A_i, B_i\).

Find the minimum possible value of \(\max_{i=1}^{ N-1} \mid A_i - A_{i+1} \mid.\)

  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 \(A_1, A_2, \dots, A_N\) - denoting the elements of the array \(A.\)
    • The third line contains \(N\) space-separated integers \(B_1, B_2, \dots, B_N\) - denoting the elements of the array \(B.\)

Output format
For each test case, print  the minimum possible value of \(\max_{i=1}^{ N-1} \mid A_i - A_{i+1} \mid.\)

Constraints

\(1 \leq T \leq 10^4 \\ 2 \leq N \leq 10^5\\ 1 \leq K \leq N\\ \\1\leq A_i \le 10 ^{9} \\ Sum\; of \; N\;over\;all\;test\;cases\;does\;not\;exceed\;2\cdot 10^5. \)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
5 votes
Tags:
Introduction to Dynamic Programming 1Number theoryArraysAlgorithmsDynamic Programming
Points:30
7 votes
Tags:
Ad-HocApprovedBasic ProgrammingBrute ForceDynamic ProgrammingMediumOpenSieve
Points:30
11 votes
Tags:
AlgorithmsApprovedCompletedMediumOpen