A ground-control team operates a ring of communication relay satellites orbiting in a fixed loop. The satellites are numbered 0 to n-1 in the order they appear around the ring, and for each satellite i there is a known laser-link length connecting it to the next satellite (i+1) mod n going around the ring in the fixed direction. Because the ring is circular, any two satellites are connected by two possible arcs (clockwise and counter-clockwise), and the team always routes a signal along whichever arc is shorter.
Given the link lengths and two relay indices, compute the length of the shorter arc between them.
Line 1: a single integer n, the number of relays.
Line 2: n space-separated integers link[0] ... link[n-1], where link[i] is the link length from relay i to relay (i+1) mod n.
Line 3: two space-separated integers start and dest, the indices of the two relays to connect.
Print a single integer: the length of the shorter of the two arcs connecting relay start and relay dest.
2 <= n <= 1000 <= link[i] <= 100 for every i0 <= start, dest < nstart != destExample 1
Input
4 1 2 3 4 0 1
Expected
1
Explanation
The ring has 4 relays with link lengths [1,2,3,4] and total ring length 10. Going from relay 0 to relay 1 directly uses link[0]=1. The other way around uses 2+3+4=9. The shorter arc is 1.
Example 2
Input
4 1 2 3 4 0 2
Expected
3
Explanation
Going from relay 0 to relay 2 the short way uses link[0]+link[1]=1+2=3. The other way around uses link[2]+link[3]=3+4=7. The shorter arc is 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →