Let n be the length of the input array. Then the number of times "distance" is computed is n(n-1)/2, which is derived from (n-1) + (n-2) + ... + 1.