Search code examples
algorithmmathaveragemedian

When these two elements will be equal/unequal : nearest element to average vs median?


I understand that median element is calculated based on index/total number of elements in an Array while Average is based on total value of the array. I am trying to solve the question : Given an integer K and a matrix of N rows and M columns, the task is to find the minimum number of operations required to make all the elements of the matrix equal. In a single operation, K can be added to or subtracted from any element of the matrix.

Which element we should take here (to which all the elements will be made equal in minimum steps) and why? I am more interested in understanding the why part.


Solution

  • Average value provides minimal sum of absolute distances from it to all elements, so it is suitable here.