The first overtake

1.6

11 votes
Basic Math, Algorithms, Math
Problem

In a car race, there are N cars 1,2 ... N where the ith car is moving with a speed of (N - i + 1). All the cars are moving in the same direction.

The current position of the cars is Xi. Determine the time at which the first overtake takes place.

Note: Overtake is defined as the place when a car crosses another car that is ahead of it.

Input format

  • Each test contains multiple test cases.
  • The first line contains the number of test cases T.
  • The first line of each test case contains two space-separated integers N and M representing the number of cars and the length of the circular path.
  • The second line of each test case contains N integers where the ith integer is Xi representing the position of the ith car on the track.

Output format

For each test case, print a single line containing one integer representing the time at which the first overtake takes place.

Constraints

1T1042N1051M1090Xi109Xi<Xi+1 for (1iN1)It is guaranteed that the sum of N over all test cases does not exceed 2105

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

The first overtake takes place at time t = 1.

Editor Image

?