반응형

Given a sorted array of integers a, find such an integer x that the value of


abs(a[0] - x) + abs(a[1] - x) + ... + abs(a[a.length - 1] - x)

is the smallest possible (here abs denotes the absolute value).

If there are several possible answers, output the smallest one.


code>>


int absoluteValuesSumMinimization(std::vector<int> a) {

return a[(a.size() - 1) / 2];

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

34>extractEachKth  (0) 2017.07.31
33>stringsRearrangement  (0) 2017.07.31
31>depositProfit  (0) 2017.07.06
30>circleOfNumbers  (0) 2017.07.06
29>chessBoardCellColor  (0) 2017.07.06

+ Recent posts