Skip to content

Instantly share code, notes, and snippets.

@Underdoge
Created March 6, 2017 06:33
Show Gist options
  • Save Underdoge/629166ed1a0d90c4c960960b0032f035 to your computer and use it in GitHub Desktop.
Save Underdoge/629166ed1a0d90c4c960960b0032f035 to your computer and use it in GitHub Desktop.
absoluteValuesSumMinimization
function absoluteValuesSumMinimization(a) {
var x=a[0],temp=a.reduce((acc,curr)=>{
return acc+Math.abs(curr-x);
},0);
for(var i=1;i<a.length;i++){
if(a.reduce((acc,curr)=>{return acc+Math.abs(curr-a[i]);},0)<temp){
temp=a.reduce((acc,curr)=>{return acc+Math.abs(curr-a[i]);},0);
x=a[i];
}
}
return x;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment