AmazonOA #224
Get Minimum Time
Estimated MediumJudge-ready
Topics (AI-labelled)In-place ArraysGreedy
Servers 1..total form a ring with unit-length edges. Choose any starting server and visit all target servers along one continuous sequential route; crossing each edge takes 1 unit of time, and this is not a simultaneous broadcast in both directions. Targets are distinct.
Input
The first line contains n total; the second line contains n target IDs. 1≤total≤10^9, 1≤n≤min(total,100000), 1≤ID≤total. The original constraint ID≤n conflicts with the statement and all examples, so it is restored to ≤total according to the statement.
Output
Output a single integer, the answer.
Example 1
Input:
3 8
2 6 8
Output:
4
Example 2
Input:
2 5
1 5
Output:
1
Example 3
Input:
1 10
4
Output:
0
Example notes
The answers are 4, 1 and 0. With a single target, simply start there.
Editorial
Loading solution…