Reputation: 1100
Here is the Challenge:
https://www.hackerrank.com/challenges/mini-max-sum/problem
Despite my answer is returning the same number that matches the expected result, I have done something wrong because my answer has been rejected. How can I solve it?
Here is the solution I had tried:
function miniMaxSum(arr) {
var arrClone1 = arr.slice()
var arrClone2 = arr.slice()
var arrMinor = arrClone1.sort(function(a, b){return a - b;})
arrMinor.pop()
var arrMajor = arrClone2.sort(function(a, b){return b - a;})
arrMajor.pop()
function getSum(a, b) {
return a + b;
}
var result1 = arrMinor.reduce(getSum)
var result2 = arrMajor.reduce(getSum)
console.log(`${result1} ${result2}`) // it is returning: 10 14
Upvotes: 4
Views: 13480
Reputation: 29
solution in fewest possible lines, i think
function miniMaxSum(arr) {
arr.sort((a,b)=>a-b);
let fSum=0, lSum=0;
for(let x=0; x<arr.length-1; x++){
fSum+=arr[x]
}
for(let x=1; x<arr.length; x++){
lSum+=arr[x]
}
console.log(fSum+" "+lSum);
}
Upvotes: -1
Reputation: 1
function miniMaxSum(arr) {
let sorted = arr.sort();
let min = 0;
let max = 0;
for(let i =1; i<arr.length; i++){
max += arr[i];
}
for(let i =0; i<arr.length -1; i++){
min += arr[i];
}
console.log(min,max)
}
Upvotes: 0
Reputation: 11
This is my solution, I hope it works for you
function miniMaxSum(arr) {
// Write your code here
const max = arr.sort((a,b) => b-a).slice(0,4).reduce((a,b)=> a+b,0)
const min = arr.sort((a,b) => b-a).reverse().slice(0,4).reduce((a,b)=> a+b,0)
console.log(min,max)
}
Upvotes: 1
Reputation: 320
This solution uses only single loop and also no utility functions. code is pretty much self-explanatory.
function miniMaxSum(arr) {
// Write your code here
const total = arr[0] + arr[1] + arr[2] + arr[3] + arr[4];
let max, min;
for (let i = 0; i < arr.length; i++) {
// Add all elements and subtract one element so that we sum of 4 items only
let sum = total - arr[i];
// Sets min & max both equal to sum on first iteration
if (max === undefined && min === undefined) {
max = sum;
min = sum; `enter code here`
} else if (max < sum) {
max = sum
} else if (min > sum) {
min = sum
}
}
console.log(min, max)
}
Upvotes: 0
Reputation: 1
function miniMaxSum(arr) {
let arrMin = [].concat(arr).sort()
let arrMax = [].concat(arr).sort()
arrMin.pop();
arrMax.shift();
let arrMinReduced = arrMin.reduce((prev, curr) => prev + curr)
let arrMaxReduced = arrMax.reduce((prev, curr) => prev + curr)
console.log(arrMinReduced, arrMaxReduced)
}
miniMaxSum([1,2,3,4,5])
Upvotes: 0
Reputation: 35
Here's a single loop variant.
function miniMaxSum(arr) {
let min = 0
let max = 0
let sum = 0
for (const n of arr) {
sum += n
if (!min || min > n) {
min = n
continue
}
if (max < n) {
max = n
}
}
console.log(sum - max, sum - min)
}
Upvotes: 0
Reputation: 14
you can shorten the code
function miniMaxSum(arr) {
// Write your code here
arr.sort((a,b) => a-b)
let min= 0, max = 0;
for(let i = 0; i < arr.length-1 ; i++){
min += arr[i];
}
for(let j = 1; j < arr.length; j++){
max += arr[j];
}
console.log(min,max);
}
Upvotes: 0
Reputation: 7
const arr = [1, 2, 3, 4, 5];
let len = arr.length;
let smallest = arr[0];
let largest = arr[0];
let minSum = 0;
let maxSum = 0;
function minMax(arr, len){
for(let i = 0; i<len; i++){
if(arr[i] >= largest){
largest = arr[i];
}
if(arr[i] <= smallest){
smallest = arr[i];
}
}
for(let i = 0; i < len; i++){
if(arr[i] > smallest){
maxSum += arr[i];
}
if(arr[i] < largest){
minSum += arr[i];
}
}
return console.log(minSum, maxSum)
}
minMax(arr, len)
Upvotes: -1
Reputation: 11
Here's a more procedural solution to the problem.
function miniMaxSum(arr) {
let sum_min = 0
let sum_max = 0
let min_val = arr[0]
let max_val = arr[0]
let sum = 0
for(let index = 0; index < arr.length; index += 1){
if(arr[index] > max_val) {
max_val = arr[index]
}
if(arr[index] < min_val){
min_val = arr[index]
}
sum = sum + arr[index]
}
sum_min = sum - max_val
sum_max = sum - min_val
console.log(sum_min, sum_max)
}
Upvotes: 1
Reputation: 1
Simple Solution (I hope), there was not any problem with 'arr' argument with me.
function miniMaxSum(arr) {
// Write your code here
let a=0, b=0;
arr.sort();
for(let i=0; i<4; i++) {
a+=arr[i];
b+=arr[i+1];
}
console.log(a,b)
}
Upvotes: 0
Reputation: 880
code worked for me is as below:
function miniMaxSum(arr) {
// Write your code here
let min = Math.min(...arr);
let max = Math.max(...arr);
let arrExceptMin, arrExceptMax;
let allEqual = arr.every(val => val === arr[0]);
if(allEqual) {
return console.log(sum(arr.slice(1)) + ' ' + sum(arr.slice(1)));
}
if(min) {
arrExceptMin = arr.filter(val => val !== min);
}
if(max) {
arrExceptMax = arr.filter(val => val !== max);
}
return console.log(sum(arrExceptMax) + ' ' + sum(arrExceptMin));
}
Upvotes: 0
Reputation: 11
function miniMaxSum(arr) {
let arrayMin = arr.slice() //new array for minimum
let arrayMax = arr.slice() //new array for maximum
let small = arrayMin.sort((a,b) => {return a - b}) //sort number small to big
let big = arrayMax.sort((a,b) => {return a - b}) //sort number small to big
function maxsum (a,b){return a + b} // that's function for calculate all numbers
let min = small.pop() //remove last element
let max = big.shift() //remove first element
let mins = arrayMin.reduce(maxsum) //apply maxsum function to array
let maxs = arrayMax.reduce(maxsum) //apply maxsum function to array
console.log(`${mins} ${maxs}`)
}
Upvotes: 0
Reputation: 1
this is my solution
function miniMaxSum(arr) {
// Write your code here
var sums = [];
for (var i=0; i<arr.length; i++) {
var num = arr.shift();
var sum = arr.reduce(function(acc, val) { return acc + val; }, 0);
sums.push(sum);
arr.push(num)
}
console.log(Math.min(...sums) + " " + Math.max(...sums));
}
Upvotes: 0
Reputation: 321
my solution:
let sumValue = arr.reduce((a, b) => {
return a + b;
});
const min = sumValue - Math.max(...arr);
const max = sumValue - Math.min(...arr);
const result = `${min} ${max}`
console.log(result);
Upvotes: 1
Reputation: 21
function miniMaxSum(arr) {
// Write your code here
let sum=arr.reduce((a,b)=>{
return a+b;
});
const min=sum-Math.max(...arr);
const max=sum-Math.min(...arr);
console.log(min+" "+max);
}
Upvotes: 2
Reputation: 1100
I found the answer. I noticed that it was mandatory to name the function argument as 'input' instead of 'arr'. That's why the answer was rejected by the HackerRank platform despite the code returned the right result in my editor, NOT in the HackerRank platform. If you do this simply adjustment, it works in the HackerRank platform too.
Just like that:
function miniMaxSum(input) { //'input' NOT 'arr'
var arrClone1 = input.slice() //'input' NOT 'arr'
var arrClone2 = input.slice() //'input' NOT 'arr'
//... rest of the code omitted
Upvotes: 2
Reputation: 6209
Here is my solution --> This will handle duplicate and float values as well
Check the live demo below:
function miniMaxSum(arr) {
let tempArr=[];
var sum = arr.reduce((acc,cur)=>acc+cur);
arr.map((val)=>{
tempArr.push(Number(sum-val));
});
// unique values only
tempArr = [...new Set(tempArr)];
console.log(`${Math.min.apply(null,tempArr)} ${Math.max.apply(null,tempArr)}`);
}
miniMaxSum([7,69,2,203,894]);
Upvotes: 0
Reputation: 1
guys. Just sharing my solution!
function miniMaxSum(input) {
input.sort((a,b) => a-b)
let min = 0, max = 0;
for(let i=0; i < input.length; i++)
{
min += input[i]
max += input[i]
}
console.log((min - input[input.length -1]) + ' ' + (max - input[0]))
}
Upvotes: 0
Reputation: 1
function miniMaxSum(arr) {
let sortarr = arr.sort();
let maxSum = 0;
let minSum = 0;
for (let i=0 ; i < arr.length - 1; i++ ){
minSum += sortarr[i];
}
for (let j=arr.length - 1; j > 0; j-- ){
maxSum += sortarr[j];
}
console.log(minSum + ' ' + maxSum);
}
Upvotes: 0
Reputation: 105
function miniMaxSum(input) {
let minElem = 0, maxElem = 0, sum = 0;
minElem = input[0];
maxElem = minElem;
sum = minElem;
for (let i = 1; i < input.length; i++) {
sum += input[i];
if (input[i] < minElem) {
minElem = input[i];
}
if (input[i] > maxElem) {
maxElem = input[i];
}
}
let minresult = sum - maxElem;
let maxresult = sum - minElem;
console.log(minresult + " " + maxresult);
}
Upvotes: 0
Reputation: 1
let numbers = arr.slice('').sort();
let maxScore = 0;
let minScore = 0;
for(let i = 0; i < numbers.length - 1; i++) {
minScore += numbers[i];
};
for(let j = 1; j < numbers.length; j++) {
maxScore += numbers[j];
};
console.log(`${minScore} ${maxScore}`);
Upvotes: 0
Reputation: 41
Another solution !
const numbers = arr.slice('').sort();
let min = 0;
let max = 0;
for (let i = 0; i < numbers.length; i++) {
if (i < 4) {
min = min + numbers[i];
}
if (i > 0 && i < 5) {
max += numbers[i];
}
}
console.log(`${min} ${max}`);
Upvotes: 0
Reputation: 111
I think the solution should be easier:
function miniMaxSum(arr) {
let sum = arr.reduce((a, b) => a + b);
let maxVal = Math.max(...arr);
let minVal = Math.min(...arr);
console.log((sum - maxVal) + ' ' + (sum - minVal));
}
Upvotes: 11
Reputation: 1
Try this, it works for all cases:
function miniMaxSum(arr) {
let c = arr.sort();
let a = c.slice(0,4)
let b = c.slice(1,5)
console.log(a.reduce((p,n)=>p+n,0),b.reduce((p,n)=>p+n,0))
}
Upvotes: 0
Reputation: 620
Using .reduce:
const arr = [1, 2, 3, 4, 5];
function miniMaxSum(arr) {
const res = arr.sort((a,b) => a-b).reduce((prev, cur, i) => {
if(i!=0) ( prev.max=prev.max+cur || cur);
if(i!=arr.length-1) ( prev.min=prev.min+cur || cur);
return prev;
}, [{max:0},{min:0}]);
console.log(res.min || 0, res.max || 0);
}
miniMaxSum(arr) // 10 14
Upvotes: 0
Reputation: 21
Here is another solution...
function miniMaxSum(arr) {
let minValue = 0, maxValue = 0, minIndex = 0, maxIndex = 0, minSum = 0, maxSum = 0;
minValue = Math.min(...arr);
maxValue = Math.max(...arr);
minIndex = arr.indexOf(minValue);
maxIndex = arr.indexOf(maxValue);
for (let i = 0; i < arr.length; i++){
if (minIndex != i) {
maxSum += arr[i];
}
if (maxIndex != i) {
minSum += arr[i];
}
}
console.log(minSum, maxSum);
}
miniMaxSum([1,2,3,4,5]);
Upvotes: 0
Reputation: 186
You've got it right. The only "problem" is, that you're doing a Java or C++ coding challenge. (That's why they're mentioning the 32 bit integer). The input shouldn't be an array but "A single line of five space-separated integers."
Upvotes: 0