We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
function miniMaxSum(arr: number[]): void {
let greaterValue = arr[0];
function miniMaxSum(arr: number[]): void {
let greaterValue = arr[0];
let smallerValue = arr[0];
let sum = 0
for( let index =0 ; index < arr.length ; index ++ ){
const value = arr[index];
greaterValue = greaterValue > value ? greaterValue : value;
smallerValue = smallerValue < value ? smallerValue : value;
sum += value;
}
let result = [sum - greaterValue , sum-smallerValue];
console.log( result.join(" ") )
}
this is simple clean approach in what it does lets consider sceanario in array we have all value there will be one small value and one greater value if we add all number and minus the greater value we will get smaller number if we minus the smaller value we will get greater Value
let result = [sum - greaterValue , sum-smallerValue];
console.log( result.join(" ") )
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Mini-Max Sum
You are viewing a single comment's thread. Return to all comments →
function miniMaxSum(arr: number[]): void { let greaterValue = arr[0];
function miniMaxSum(arr: number[]): void { let greaterValue = arr[0]; let smallerValue = arr[0]; let sum = 0
for( let index =0 ; index < arr.length ; index ++ ){ const value = arr[index]; greaterValue = greaterValue > value ? greaterValue : value; smallerValue = smallerValue < value ? smallerValue : value; sum += value; }
let result = [sum - greaterValue , sum-smallerValue]; console.log( result.join(" ") ) }
let result = [sum - greaterValue , sum-smallerValue]; console.log( result.join(" ") ) }