I have an array int [] nums = {5, 1, 6, 10, 4, 7, 3, 9, 2}
int [] nums = {5, 1, 6, 10, 4, 7, 3, 9, 2}
I want to find the distance between the smallest and largest number in that array in O(n^2) time.
For numbers (as opposed to points on a plane) You can do that in linear time. Find maximum, find minimum, then subtract. So, no nested loops required.