I found a problem that stated:
Let consider a vector x = (x1, x2 ... xn) with real elements. 1). Sort the vector
I don't know if sorting an array helps, but for 2). I thought I could do the arithmetic sum of all the elements in the vector and say that avg is what we were looking for.
But this is not true. Example:
x = (1, 10, 10) avg = [ 21/3 ] = 7 = a sum = |1 - 7| + |10 - 7| + |10 - 7| = 6 + 3 + 3 = 12
but if we consider a = 10, we get
sum = |1 - 10| + |10 - 10| + |10 - 10| = 9 < 12
Another solution that I could think of would be brute force from the min element to the highest with a step i += 0.1
source share