Absolute Values Sum Minization

Absolute Values Sum Minization


Given a sorted array of integers a, find an integer x from a such that the value of abc(a[0] - x) + abs(a[1] - x) + … + abc(a[a.length - 1] - x) is the smallest possible (here abc denotes the absolute value). If there are several possible answers output the smallest one.

Example

For a = [2, 4, 7], the output should be absoluteValuesSumMinimization(a) = 4.

For a = [2, 4, 7, 6], the output should be absoluteValuesSumMinimization(a) = 4.

For a = [2, 4, 7, 6, 6], the output should be absoluteValuesSumMinimization(a) = 7.

For a = [2, 4, 7, 6, 6, 8], the output should be absoluteValuesSumMinimization(a) = 7.

function absoluteValuesSumMinimization(x) {
	const isEven = x.length % 2 === 0;

	return isEven ? x[x.length / 2 - 1] : x[Math.floor(x.length / 2)];

} 


  1. First we check if the number is even.
  2. If the number is even we take the length of the array of numbers and divide by 2 and then - 1;
  3. Else if the length of the array is odd number we use the second else part where we Math.floor for full number and we divide by 2.

First we create function in our case absoluteValuesSumMinimization with only one parameter named x , and a temporary variable isEven

First we check if the number is even.
isEven = x.length % 2 === 0


If the number is even we take the length of the array of numbers and divide by 2 and then - 1;
return isEven ? x[x.length / 2 - 1] : x[Math.floor(x.length / 2)];

comments powered by Disqus