minimum and maximum values that can be calculated
Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
function miniMaxSum(arr) {
let sum=0
for(let i=0;i<arr.length; i++){
sum += arr[i]
}
let minus = []
for(let j=arr.length;j>=0; j--){
let minusSum = sum
minus.push(sum - arr[j])
}
let smallest = Infinity;
let greatest = -Infinity;
for (let i = 0; i < minus.length; i++) {
if (minus[i] < smallest) {
smallest = minus[i];
}
if (minus[i] > greatest) {
greatest = minus[i];
}
}
console.log(smallest, greatest);
Comments
Post a Comment