Today I have solved problem in javascript
You are given an array X of floating-point numbers x1, x2, ... xn. These can be rounded up or down to create a corresponding array Y of integers y1, y2, ... yn.
Write an algorithm that finds an appropriate Y array with the following properties:
The rounded sums of both arrays should be equal.
The absolute pairwise difference between elements is minimized. In other words, |x1- y1| + |x2- y2| + ... + |xn- yn| should be as small as possible.
For example, suppose your input is [1.3, 2.3, 4.4]. In this case you cannot do better than [1, 2, 5], which has an absolute difference of |1.3 - 1| + |2.3 - 2| + |4.4 - 5| = 1.
const inputArray = [1.3, 2.3, 4.4];
const resultArray = roundArray(inputArray);
function roundArray(inputArray) {
const n = inputArray.length;
const roundedArray = [];
let roundedSum = 0;
for (let i = 0; i < n; i++) {
consat roundedValue = Math.round(inputArray[i]);
roundedArray.push(roundedValue);
roundedSum += roundedValue;
}
for (let i = 0; i < n; i++) {
if (roundedSum < inputArray.reduce((acc, val) => acc + Math.ceil(val), 0)) {
roundedArray[i] = Math.ceil(inputArray[i]);
} else {
roundedArray[i] = Math.floor(inputArray[i]);
}
}
return roundedArray;
}
console.log(resultArray);
Output:
[ 2, 3, 5 ]
Comments
Post a Comment