An array is manipulated k times so that each time the max value is divided by 2 and rounded up. I need to find its minimum sum after these k manipulations. k and all numbers in array num > 1. The method minSum receives an array called num and an integer k. The brute Python code that works for me with very bad time complexity is:
function minSum(arr, k) {
// Write your code here
let sum = 0;
while(k !==0){
let max = Math.max(...arr)
let index = arr.indexOf(max);
max = Math.ceil(max/2);
arr[index] = max;
k--;
}
sum = arr.reduce((a, b) => a + b, 0);
console.log(sum);
return sum;
}
Similar question relate to python is here. More efficient method of finding minimum sum after k operations
but nothing related to Javascript.