简体   繁体   中英

Mini-Max Sum In JavaScript - How to Get the Minimum Sum And Maximum Sum of 4 Elements in a 5-Element Array

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Here is a cleaner approach to solving this problem.

function miniMaxSum(arr) {
   let min, max, sum, arrMin, arrMax; // declare variables
   min = Math.min(...arr) // gets the smallest value from the arr
   max = Math.max(...arr) // gets the largest number from the arr
   sum = arr.reduce((a,b) => a+b, 0); // reduce used to add all values in arr
   arrMin = sum - max; // excludes the largest value 
   arrMax = sum - min; // excludes the smallest value
   console.log(arrMin+' '+arrMax) // output
}   

easy solution

const miniMax = arr => {
    let min = arr[0];
    let max = arr[0];

    for(let i = 0; i < arr.length; i++) {
        if(arr[i] <= min) {
            min = arr[i];
        }
        if (arr[i] >= max) {
            max = arr[i];
        }
    }

    let sum = arr.reduce((acc,curr) => acc + curr);

    console.log(sum - max, sum - min);  
}

miniMax([5,5,5,5,5]); 
// result : 20 20

miniMax([1,2,3,4,5]); 
// result : 10 14

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

Link to HackerRank Challenge

My idea is to loop through the array and sum all elements in the array except for one element each time, then find the smallest sum and the largest sum.

I know that .splice() can remove any element from an array. But currently, with my code, it's only removing one element from the array once . Ie this is only giving me one chunk:

function miniMaxSum(arr) {

  let smallestSum = 0;
  let largestSum = 0;

  for (let i = 0; i < arr.length; i++) {
    let chunk = arr.splice(1);
    console.log(chunk);
    if (chunk > largestSum) largestSum = chunk;
    if (chunk < smallestSum) smallestSum = chunk;
  }
  return (smallestSum, largestSum);
}

I need to remove one element from the array every time while looping, then get the max and min sums from that array.

So for given array of [1, 2, 3, 4, 5]

I should get the following possible "chunks":

[2, 3, 4, 5] , [1, 3, 4, 5] , [1, 2, 4, 5] , [1, 2, 3, 5] , [1, 2, 3, 4] .

The chunk with the highest sum is [2, 3, 4, 5]

And the chunk with the smallest sum is [1, 2, 3, 4] .

How can I adjust my code to get all of the possible 4-digit arrays within the given array so that I can compare their sums, still using a for-loop? Or if not with a for-loop, what else would you suggest?

EDIT : Now using Math.min() and Math.max() to get the smallest and largest elements in the array. Then using .filter() to remove those elements in order to create new arrays. Then getting the sums of those arrays.

function miniMaxSum(arr) {

  let smallest = Math.min(...arr);
  let largest = Math.max(...arr);

  let smallestArray = arr.filter(element => element !== largest);
  let largestArray = arr.filter(element => element !== smallest);

  let sumOfSmallestArray = 0;
  let sumOfLargestArray = 0;

  for (let i = 0; i < smallestArray.length; i++) {
    sumOfSmallestArray += smallestArray[i];
  }

  for (let i = 0; i < largestArray.length; i++) {
    sumOfLargestArray += largestArray[i];
  }

  return ([sumOfSmallestArray, sumOfLargestArray]).toString();
}

But even though it works in my console, it doesn't work in HackerRank.

function miniMaxSum(arr) {
    var soma = 0
    let t = 0
    for (var i = 0; i < arr.length; i++) {
        soma += arr[i]
        max = soma - arr[i]
    }
    let min = soma - arr[0]
    return max + ' ' + min
}

console.log(miniMaxSum([7, 69, 2, 221, 8974]))

Use build Math methods to find max and min:

function miniMaxSum(arr) {
    const min = Math.min(...arr);
    const max = Math.max(...arr);
    const sum = arr.reduce((a, b) => a + b);
    console.log(sum - max, sum - min);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM