changed here as in case of even elements there will be 2 medians
int median2 = arr . get ( ( ( n * m ) / 2 ) - 1 ) ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . abs ( arr . get ( i ) - median2 ) / k ; minOperations = Math . min ( minOperations , minOperations2 ) ; }