Maximize distance

0

0 votes
Greedy Algorithms
Problem

You are given a sorted array(arr) of size n consisting of distinct numbers from 1 to n (inclusive). You are given two positions p and q and you are allowed to do atmost k moves. A legal move is described as follows:

  • You can pick any two consecutive positions in the arr and swap their contents.

Note here consecutive means picking two positions i and j such that ij = 1

You are required to maximise the distance between arr[p] and arr[q] with atmost k legal moves and output the maximum distance. You have to answer t testcases.

Constraints:

1t100

2n100,0k100,1p,qn,pq

Input format:

First line is the number of testcase t to be answered. The next t lines is of the format n,k,p,q.

Output format:

Output the maximum distance possible after k swaps.

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

No explanation for problems where you need to come up with an optimal way

Editor Image

?