Find the range of numbers in a modulo field I am given a set of natural numbers $S$ that are bounded by $0$ and $360$ (they are  angles in a circle). 
My task now is to compute the range of these numbers. That is, I need to find the highest and lowest number in the set and subtract them. 
E.g. for the set of angles $S=[46, 35, 57, 70]$ the range would be $35 = 70-35$.
The tricky part comes into play when the numbers are close to $0/360$, as the distance is computed in the modulo $360$ field.
E.g. for the set of angles $S=[355, 358, 2, 4]$ the range is $9 = (4-355) $ % $ 360$.
I need to solve this with an implementation, so I need a structural approach how to compute the range without trying out all combinations of possible minima and maxima.
I tried to sort the array and concat it again with $360$ added to the second half, but then $\max-\min$ makes no sense anymore.
Thank you!
 A: list the angles from smallest to largest:  $a_1, a_2, a_3,......, a_n$
For $k< n$ then difference $a_{k+1}-a_k = d_k$ is how for to go from angle $a_k$ to $a_{k+1}$ and $r_k = 360-d_k$ is the range of going the other way around the circle from $a_{k+1}$ through all the angles to point $a_k$.
And for $k =n$ then range of going from $a_n$ around the circle down through all the numbers to $a_1$ is $r_n = a_n - a_1$.
The smallest $r_k$ is the the  range you want.
A: What you want is to check the "distance" between each set of values to determine the largest value among them as the "range" of distances. In this case, the distance is either directly between the $2$ values, or going across the $360$ boundary. It's like they are angles of points on a circle and you want to get the minimum angle between those points (i.e., the range you're looking for is the smallest angle of an arc which covers all of the points), which is what I'm assuming this is basically for.
An alternative way to get basically the same thing is to take the largest distance between any $2$ points and then use $360$ less that. This uses the largest gap between any $2$ points as the angle of the curve outside of the range of degrees to give the range the smallest possible value, including using wrap-around if need be.
A way to do that is of order $n\log(n)$ since it involves sorting, but it's better than $n^2$ if you compare each value to each other one in an unsorted array. Thus, first, sort the values into increasing order. Then starting from the first one, subtract each next number from the previous one. At the final value, use the first value plus $360$ as the next value to subtract from. The range is $360$ less the largest among these values.
With your first example of $S=[46, 35, 57, 70]$, the sorted array is $S_1 = [35, 46, 57, 70]$. The values you now get are $11$, $11$, $13$ and $265$. Thus, the range is $360 - 265 = 35$, as you got.
Using your second example of $S=[355, 358, 2, 4]$, the sorted array is $S_1 = [2, 4, 355, 358]$. The values you get here are $2$, $351$, $3$ and $4$. Thus, the range is $360 - 351 = 9$, which also matches your expectation of $9 \equiv 4 - 355 \pmod{360}$.
Here is a slightly larger, perhaps more interesting example, already sorted in increasing order. Let $S = [2, 50, 125, 190, 240, 260, 305]$. In this case, the values to check now become $48$, $75$, $65$, $50$, $20$, $45$ and $57$. The range here is $360 - 75 = 225$, with this going between $125$ increasing until it goes past $360$, to then switch to $0$ and keep increasing until you get to $50$. Alternatively, but equivalently, you can view it as $50$ decreasing in values until it goes below $0$, to then switch to $360$ and keep decreasing until you get to $125$.
