find a number for minimum sum of absolute difference in an array

Viewed 1600

for example, array a[]= {1,1,10}, we need to find 'x' such that |x-1|+|x-1|+|x-10| is minimum.
here, x is 1.

Could it be solved in greedy approach, like taking average or something else?
Note: taking average doesn't work, why?

I can only come up with O(nlogn) solution (binary search), is there any other approach like dp?

Thanks in advance!

1 Answers
Related